SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Case When Sum
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

masond
Constraint Violating Yak Guru

447 Posts

Posted - 09/11/2013 :  10:35:32  Show Profile  Reply with Quote
HI Guys
Really quick question
Aim – to produce two columns and within them columns have the sales number for that given month

In the example below, i would like to sum May sales and June Sales

SELECT Dim_Outlet.FDMSAccountNo,
sum(hst_sales_amt) as Sales
FROM Fact_Financial_History INNER JOIN
Dim_Outlet ON Fact_Financial_History.hst_merchnum = Dim_Outlet.FDMSAccountNo_First9
where Plan_Key in ('85-122',
'85-123',
'85-124',
'85-125',
'85-126',
'85-127')
group by FDMSAccountNo

Edited by - masond on 09/11/2013 10:46:08

MuMu88
Aged Yak Warrior

547 Posts

Posted - 09/11/2013 :  11:55:23  Show Profile  Reply with Quote
Something like this:



SELECT Dim_Outlet.FDMSAccountNo,
sum(hst_sales_amt) as Sales,
SUM(CASE WHEN [SALESDATE] BETWEEN '20120101' AND '20120131' THEN  hst_sales_amt ELSE 0 END) AS January2012Sales, 
SUM(CASE WHEN [SALESDATE] BETWEEN '20120201' AND '20120229' THEN  hst_sales_amt ELSE 0 END) AS February2012Sales
FROM Fact_Financial_History INNER JOIN
Dim_Outlet ON Fact_Financial_History.hst_merchnum = Dim_Outlet.FDMSAccountNo_First9
where Plan_Key in ('85-122',
'85-123',
'85-124',
'85-125',
'85-126',
'85-127')
group by FDMSAccountNo


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 09/12/2013 :  16:48:37  Show Profile  Reply with Quote
is it always May and June or do you need current and prev months?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000