Solved

SQL Syntax

Posted on 2010-11-30
8
238 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

13 Experts available now in Live!

Get 1:1 Help Now