Solved

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

Posted on 2016-09-12
3
54 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 51

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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

17 Experts available now in Live!

Get 1:1 Help Now