Solved

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

Posted on 2016-09-12
3
163 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
[X]
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
3 Comments
 
LVL 23

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 58

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

Industry Leaders: 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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

617 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