Solved

SQL Syntax

Posted on 2010-11-30
8
247 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
  • 4
  • 3
8 Comments
 
LVL 142

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
 
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now