Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-12
3
Medium Priority
?
285 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 23

Accepted Solution

by:
Ferruccio Accalai earned 2000 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 60

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

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!

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

886 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