SQL Running Total using CROSS JOIN -


need running total of table based on date , column. have following table called sales:

day      client  1        smith, j 3        johnson, b 6        fuller, 7        smith, j 8        johnson, b 9        lee, m 

i run following query:

select a.day, a.client, sum(1) runningtotal sales cross join sales b (b.day <= a.day)  group a.day, a.client order a.day 

this gives me following:

day client       runningtotal  1   smith, j     1 3   johnson, b   2 6   fuller,    3 7   smith, j     4 8   johnson, b   5 9   lee, m       6 

but half correct. want running total based on day , client. table want looks this:

day client       runningtotal  1   smith, j     1 3   johnson, b   1 6   fuller,    1 7   smith, j     2 8   johnson, b   2 9   lee, m       1 

don't need add client join?

select a.day, a.client, sum(1) runningtotal sales  join sales b on b.day <= a.day , a.client = b.client  group a.day, a.client order a.day 

btw: approach calculating running totals suitable small input sets. work required grows exponentially.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -