Solved

MySQL Query - How to Get Data From Current Month and Same Month Last Year?

Posted on 2016-09-12
3
102 Views
Last Modified: 2016-09-12
I have a simple query that sums sales totals for September and compares them with Sept of last year. How can I write this so that it auto updates with the current month month each month?

Select Salesman,
 SUM(CASE WHEN InvoiceDate Between '2015-9-1' and '2015-9-30' THEN Totsales else 0 end) as Sales15,
 SUM(CASE WHEN InvoiceDate Between '2016-9-1' and '2016-9-30' THEN Totsales else 0 end) as Sales16
 from vCustomerInvoiceDetail
Group by Salesman

Open in new window


This is part of a PHP script so if there's a better/easier way to do it in PHP I'd be happy to hear about that as well.

Thank You!
0
Comment
Question by:dannyg280
3 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 500 total points
ID: 41794494
You could use a query where you select just by year and month instead of using between starts and stop. This way you could pass as parameter a comparing date like CURRENT_DATE. The query could be explained so: select the sum for any date with the same month and year of today and the sum for any date with the same month of today but 1 year less.

Take a look at the query
SELECT  Salesman, 
 SUM(CASE WHEN (MONTH(InvoiceDate) = MONTH( CURRENT_DATE ) AND YEAR(InvoiceDate) = YEAR( CURRENT_DATE ) ) THEN Totsales ELSE 0 END) AS Sales16,
 SUM(CASE WHEN (MONTH(InvoiceDate) = MONTH( CURRENT_DATE ) AND YEAR(InvoiceDate) = YEAR( CURRENT_DATE - INTERVAL 1 YEAR ) ) THEN Totsales ELSE 0 END) AS Sales15
from vCustomerInvoiceDetail
Group by Salesman

Open in new window


If you need you could replace the CURRENT_DATE value with any other date by php and pass it to the query by code
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41794681
I think this is what you asked for - not sure it is what you want. For this to mean anything it would need to run on the last day of the month. Otherwise if you want to look back at the last month it would require some tweaking.
SELECT Salesman,
 SUM(CASE WHEN InvoiceDate BETWEEN CONCAT(YEAR(NOW())-1, '-', MONTH(NOW()), '-01') AND CONCAT(YEAR(NOW())-1, '-', MONTH(NOW()), '-', DAY(LAST_DAY(NOW()))) THEN Totsales ELSE 0 END) AS Sales15,
 SUM(CASE WHEN InvoiceDate BETWEEN CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-01') AND CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', DAY(LAST_DAY(NOW()))) THEN Totsales ELSE 0 END) AS Sales16
 FROM vCustomerInvoiceDetail
GROUP BY Salesman

Open in new window

0
 

Author Closing Comment

by:dannyg280
ID: 41794749
Worked great! Thank You!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 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