Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Case When Sum

Author  Topic 

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2013-09-11 : 10:35:32
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

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-09-11 : 11:55:23
Something like this:
[CODE]


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

[/CODE]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-12 : 16:48:37
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
   

- Advertisement -