I have the following data in my database: SALES DATE SID Sales 1/1/2008 111 1 1/1/2008 111 1 1/1/2008 222 1 1/1/2008 333 1 1/2/2008 111 0 1/2/2008 222 4 1/2/2008 222 7 1/2/2008 333 9 1/3/2008 111 6 1/3/2008 111 4 1/3/2008 222 1 1/3/2008 333 8 I need to one query to get Results 1 and one query to get Results 2: Results 1 DATE SID Total Sales 1/1/2008 111 2 1/1/2008 222 1 1/1/2008 333 1 1/2/2008 111 0 1/2/2008 222 11 1/2/2008 333 9 1/3/2008 111 10 1/3/2008 222 1 1/3/2008 333 8 Results 2 DATE SID Cumulative Total Sales 1/1/2008 111 2 1/1/2008 222 1 1/1/2008 333 1 1/2/2008 111 2 1/2/2008 222 12 1/2/2008 333 10 1/3/2008 111 12 1/3/2008 222 13 1/3/2008 333 18 Notice how Results 2 is the cumulative of the previous dates from Result 1's rows for each SID. Any ideas or help is greatly appreciated. Thanks!
-- Result 1's query; Select DATE, SID, SUM(SALES) as SALES From #Sales Group By DATE, SID Order By DATE, SID ---------------------------------------------------- -- Result 2's query; Select t1.DATE, t1.SID, Isnull(SUM(t1.SALES) + ( Select SUM(SALES) as CumulativeSales From #Sales Where DATE < t1.DATE and SID = t1.SID Group by SID ), SUM(t1.SALES)) as CumulativeSales From #Sales t1 Group By t1.DATE, t1.SID Order By t1.DATE, t1.SID
Thanks tnrsr. The first query worked, but for the second query I get the following error telling me to check the syntax used near 'SUM(t1.Sales)) as CumulativeSales from #Sales t1 Group By t1.DATE, t1.SID" Any ideas what could be wrong? Thanks again!
I got the exact "Result 2" results that you asked with executing this query. I'm pretty sure that you did it but, you changed the query towards your tables right? I mean you replaced #Sales with XXXXX (your table name)..
Honestly, i don't. Cause i didn't get any errors like you said. I got the exact result that you asked.