I have a temporary table (#Temptable3) I'm populating to assist building a dataset to report from. I've solved most logical steps through a bit of trial and error and finding answers to previous questions here (thanks!) however cannot crack this last bit. To simplify, I've removed irrelevant columns from a sample dataset below:
I currently have:
RowNumber Increment Score
-----------------------------
1 1 NULL
2 100000 NULL
3 -1 NULL
4 1 NULL
5 10 NULL
6 -1 NULL
7 -100000 NULL
8 -10 NULL
What I'm aiming to get is the score column to populate with the Sum of the Increment column up to and including it's own row e.g.:
RowNumber Increment Score
-----------------------------
1 1 1
2 100000 100001
3 -1 100000
4 1 100001
5 10 100011
6 -1 100010
7 -100000 10
8 -10 0
I've tried and failed to get an various update statements to work, playing with self joins, but cannot find anything that looks promising. Apologies if this isn't enough info. Please ask questions if required Thanks for all help.
Thanks to HABO for the pointer to help me find questions on Running sums. A link in janderssons reply to a previous question lead me to a solution that worked for me:
declare @runningtotal int set @runningtotal = 0
update #TempTable3 set @runningtotal = Score = @runningtotal + Increment
from #TempTable3