Solved

MySQL Sub-query for YTD

Posted on 2014-03-23
9
896 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
  • 5
  • 3
9 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 450 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 108

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
 

Author Comment

by:Glen Gibb
ID: 39949155
Tnx.  I understand the burden March Madness can impose!  I'll wait for the SQL.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

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 48

Assisted Solution

by:PortletPaul
PortletPaul earned 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now