Jump to content

Recommended Posts

Hello guys.

 

I want to show the total sale value of each product for the past six months. But i have to separate each monthly sale with columns. For example our current month is march, 2010. So on the 1st column, i have to show the total sale for october 2009, nov '09 on the 2nd column until march 2010 on the 6th column.

 

What i have in mind is to use this mysql command:

SELECT SUM(total)
FROM sales
WHERE sale_date  BETWEEN DATE_SUB(LAST_DAY(CURDATE()), INTERVAL 6 MONTH)
            AND DATE_SUB(LAST_DAY(CURDATE()), INTERVAL 5 MONTH)

 

My concern is that will it fetch all sales from Oct 1, 2009 to Oct 31, 2009? If i change code between 5 month and 4 month interval, would it fetch all sales from Nov 1, 2009 to Nov 30, 2009?

 

Please correct my code if you have better solutions. thanks so much guys

Link to comment
https://forums.phpfreaks.com/topic/193877-help-with-query/
Share on other sites

Use the WHERE clause to get all the data for the date range you are interested in. Use a GROUP BY clause to form groups for each year_month within that range.

 

I would use the mysql EXTRACT() function to get the year_month part of the values you will need in the query - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_extract

 

 

Link to comment
https://forums.phpfreaks.com/topic/193877-help-with-query/#findComment-1020358
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.