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
Solved

MySQL Sub-query for YTD

Posted on 2014-03-23
9
929 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 109

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 109

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML extra information 8 28
MySql Recovery 2 27
PHP breaks when used in Wordpress template file 3 30
reccommendations for a free msft sql query manager? 4 26
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
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.

809 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