Solved

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

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…

815 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

12 Experts available now in Live!

Get 1:1 Help Now