Jump to content

Select seperate date ranges in one query


xander85

Recommended Posts

Hi All,

 

I have some MySQL experience, but I've been out of the "game" for a few years and I'm a bit rusty. I'm using the latest version of MySQL, but still not sure if I want I'm trying to do is possible:

 

I want to select and sum all rows for my "actual_sales" column for the last week and month in one query. Is this possible?

 

I'm currently using the following code to select the last week (ignore the date shift, my program starts it's week on Wed):

 

SELECT storeid AS store, (SELECT storenum FROM store WHERE storeid=store) AS storenum, 
(SELECT name FROM store WHERE storeid=store) AS name, wedate, SUM(actual_sales) AS sales, 
cust_count FROM `daily` WHERE wedate > '2010-03-09' AND wedate <= '2010-03-16' GROUP BY 
storeid, wedate ORDER BY sales DESC

 

And the following to return the last month:

 

SELECT storeid AS store, (SELECT storenum FROM store WHERE storeid=store) AS storenum, (SELECT name FROM store 
WHERE storeid=store) AS name, wedate, SUM(actual_sales) AS sales, cust_count FROM `daily` 
WHERE month(wedate) = (MONTH(CURDATE()) - 1)  GROUP BY storeid ORDER BY sales DESC

 

These both work fine and return the sum of the "actual_sales" column for each "storeid", but I want to know if it's possible to put these two queries together?

 

I want to be able to display the last week's sales for each store (sorted by highest sum), then compare to the last month's sales for the same store, regardless of it being sorted (because one store could have had higher sales the previous month).

 

Thanks!

 

 

Link to comment
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.