Solved

MySQL Sub-query for YTD

Posted on 2014-03-23
9
908 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
A form to still have contents even if some are wrong 10 47
PHP warning 4 30
html input type 3 23
category table 2 11
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

863 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

27 Experts available now in Live!

Get 1:1 Help Now