If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Weekly sales data query help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-21-11, 12:15
cgapperi cgapperi is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
Weekly sales data query help

I am not a complete newbie to SQL or query writing, but this one has got me stumped.

I have a weekly sales gathering database, sales_data, that comes in as such:

location_id, reporting_date, sales

I generate a report that takes the format:

location_id | week_date1 | week_date2 | .....
853 | 12000 | 13444 | .....
989 | 32400 | 36762 | .....

I have been manually building and extending a report that pulls the data by location into date columns like this:

Code:
SELECT location_id, 
SUM(if(reporting_date = '2010-11-30',sales, 0)) as '2010-11-30',
SUM(if(reporting_date = '2010-12-07',sales, 0)) as '2010-12-07'
....
FROM sales_data
GROUP BY location_id
The problem is that I don't want to continue writing and extending this report for each week date. Looking for suggestions to make the SUM(IF(...)) generate with some sort of grouping command.

Any assistance or insight on this would be helpful.
Reply With Quote
  #2 (permalink)  
Old 03-21-11, 12:24
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
consider creating a table which identifies what comprises a week then join that tabel to the main data, and it gives you the Week ID. group on that....

then parametrise your query, using, say a stored procedure. supply whatever parameter(s) you need. those parameters could be the end date AND the number of weeks.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 03-21-11, 12:33
cgapperi cgapperi is offline
Registered User
 
Join Date: Mar 2011
Posts: 2
clarification

For clarification, there is really only one entry per location per period. I am only using the sum command to pull that single entry.

I have thought of making a date dimension table, but not sure how that helps, per se, but will reread your comment to try to understand better.

Thanks for the reply.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On