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.
| Author |
Topic |
|
zoe2003
Starting Member
17 Posts |
Posted - 2012-05-17 : 09:31:38
|
| Hello all,I need your help please.I have this table:date user_id sum1/1/2011 1 51/1/2011 2 01/1/2011 3 31/2/2011 1 21/2/2011 2 41/2/2011 3 11/3/2011 1 01/3/2011 2 21/3/2011 3 2I need a query that will give me a cumulative values (for the sum column), such as:date user_id sum1/1/2011 1 51/1/2011 2 01/1/2011 3 31/2/2011 1 71/2/2011 2 41/2/2011 3 41/3/2011 1 71/3/2011 2 61/3/2011 3 6for each date&user_id it will aggregate the sum column from the beginning to this date.example: user_id=1 & date=1/3/2011 => sum=5+2+0=7.Thanks,Z. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-05-17 : 09:51:11
|
[code]select t.date, t.user_id, t.sum, c.cummfrom this_table t outer apply ( select cumm = sum(x.sum) from this_table x where x.user_id = t.user_id and x.date <= t.date ) c[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|