Solved

SQL Syntax

Posted on 2010-11-30
8
270 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
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…

726 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