?
Solved

SQL Syntax

Posted on 2010-11-30
8
Medium Priority
?
271 Views
Last Modified: 2012-05-10
I have a table that I need to write a report off of. It has 2 columns Sales and LYSales. I need to show the value of Sales for the parameter used for the week but I also need to SUM Sales and LYSales for the month(every record has a week and month field). So for instance I have this query to bring back the data I need for Sales and LYSales

Select StoreID, Store, SUM(Sales), SUM(LYSales)
from tableA
Where week = '42'
GROUP BY StoreID, Store

How could I get the all the Sales and LYSales for the month and not just the week. I was thinking a UDF that I pass in the StoreID and the Month to return the SUM for the month just not exactly sure how to do it? Or is there an easier way to extarct the Months Sales in the same query above?

Any help???
0
Comment
Question by:johnnyaction
[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
  • 4
  • 3
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34239100
can you please show some sample data from the table, and the requested output?
just to clarify...
0
 
LVL 15

Expert Comment

by:dirknibleck
ID: 34239105
You could do something like this (I assume you have a date field associated with each sale...):

Select StoreID, Store, SUM(CASE WHEN date <= monthstart and date >= monthend THEN Sales ELSE 0 END), SUM(CASE WHEN date >=yearstart and date <= yearend THEN LYSales ELSE 0)
from tableA
GROUP BY StoreID, Store
0
 
LVL 15

Expert Comment

by:dirknibleck
ID: 34239127
I notice I missed the week requirement, in which case add SUM(CASE WHEN week = '42' THEN Sales ELSE 0 END)

Also, I missed END at the end of the second CASE statement above.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 1

Author Comment

by:johnnyaction
ID: 34239372
Sample data...

Store ID      Store         week      Month      Sales $      LY Sales $
1      DT SEATTLE    38      9      12065.15      10484.9
1      DT SEATTLE         39      9      9338.4      9083.1
1      DT SEATTLE         40      10      7673.9      14020.18
1      DT SEATTLE         41      10      6502      7511.6
1      DT SEATTLE         42      10      6681.45      11544.18
0
 
LVL 15

Expert Comment

by:dirknibleck
ID: 34239398
In that case, to get week 42:


Select StoreID, Store, SUM(CASE WHEN week = '42' THEN Sales ELSE 0 END) as current_week, SUM(CASE WHEN week = '42' THEN LYSales ELSE 0 END) as last_year_week, SUM(CASE WHEN MONTH = '10' THEN Sales ELSE 0 END) as current_month, SUM(CASE WHEN MONTH='10' THEN LYSales ELSE 0 END) as last_year_month
from tableA
GROUP BY StoreID, Store
0
 
LVL 1

Author Comment

by:johnnyaction
ID: 34239459
Only problem would be is that the week would be the only parameter. So a 42 wuold be passed in and I would have to come up with the number for Sales for that week, figure out what month it is and get the Sum of Sales for the month
0
 
LVL 15

Accepted Solution

by:
dirknibleck earned 2000 total points
ID: 34239529
Try this
SELECT week.StoreID, week.Store, week.sl as current_week, week.lsl as last_year_week, month.sl as current_month, month.lsl as last_year_month

FROM 

(SELECT StoreID, Store, month, SUM(sales) as sl, SUM(LYSales) as lsl FROM tableA WHERE week ='42' GROUP BY StoreID, Store, month) as week, (SELECT StoreID, Store, month, SUM(sales) as sl, SUM(LYSales) as lsl FROM tableA GROUP BY StoreID, Store, month) as month

WHERE week.StoreID = month.StoreID AND week.month = month.month

Open in new window

0
 
LVL 1

Author Closing Comment

by:johnnyaction
ID: 34239673
nice work my friend!!
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

777 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