Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MySQL Sub-query for YTD

Posted on 2014-03-23
9
Medium Priority
?
1,007 Views
Last Modified: 2014-03-23
SubQuery.txtExperts,

I need to add a YTD column for the attached query.  

The idea is to display values for each month, but to have the sum of expenditures for the year in YTD.  Thus, the WHERE clause at the query's end does not apply to the YTD column.

So, could someone please show me:

1.  How to add a sub-query within this sql statement with the appropriate WHERE criterion to display YTD values.

2.  An easy way in either PHP or MySQL to retrieve the first day of the year in question.

Your help is much appreciated.
0
Comment
Question by:Glen Gibb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1800 total points
ID: 39949046
In PHP, the first day of the year (in ISO-8601 format) is date('Y-01-01').  This article explains a bit more about it.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 

Author Comment

by:Glen Gibb
ID: 39949055
You're always the Man, Ray!

Tnx for the quick response.

But does this formula extract the year of '2013-03-31' so I can make my new value '2013-01-01'?

And can someone show me how to insert my SELECT SUM(delivery_volume * price) WHERE open_date BETWEEN '2013-01-01' AND 'current_date'?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39949143
...formula extract the year of '2013-03-31' so I can make my new value '2013-01-01'?
I think so, if it is applied correctly.  I can't answer all of the question since I have to watch basketball with houseguests, but almost any English-language human-readable date string can be converted to a timestamp with strtotime() and any timestamp can be converted to a human-readable date with date().
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Glen Gibb
ID: 39949155
Tnx.  I understand the burden March Madness can impose!  I'll wait for the SQL.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39949162
Pretty sure someone else can drop the SQL for you.  Fing Duke cost me a billion this year!
0
 

Author Comment

by:Glen Gibb
ID: 39949173
Goes to show even a Savant can't necessarily predict the future!  But I certainly respect your knowledge in this arena.
0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 200 total points
ID: 39949501
Your current query does not identify tables with each field in the select clause, so it's not clear what tables are required for a subquery:

SELECT SUM( ???.delivery_volume * ???.price)
FROM ????  
WHERE ???.??? = ???.???
AND open_date BETWEEN '2013-01-01' AND 'current_date'?
0
 

Author Comment

by:Glen Gibb
ID: 39949619
Tnx, P...Paul, for weighing in as well.  The whole query is included in the file at the top of the page.  

You are right, however, in requiring the tables from which the values are to be selected.  That isn't at all clear from my post -- but I had sort of hoped it would be from the info in the file.

However, I've figured out how to include the SubQuery as a column:  just put it in parentheses > 

(SELECT SUM (...) FROM tbl WHERE open_date BETWEEN 'y-01-01' AND 'curr') AS YTD

That can be added to the query shown in the file w/out producing an error.

I think I can now compose the query with the info you experts have supplied.
0
 

Author Closing Comment

by:Glen Gibb
ID: 39949622
Tnx, Experts.  I've got what I needed.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question