Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Syntax

Posted on 2010-11-30
8
Medium Priority
?
273 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

596 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