I have the following table:
CREATE TABLE tbl_proc(
[proc] float,
subscriber bigint
)
data:
proc | subscriber
-----|-----------
0.7 | 123456
0.5 | 1234567
0.3 | 12345
0.3 | 45678
0.3 | 1234
0.2 | 123455
0.1 | 894562
I would like to find a nice method to add a new column to the table that represents the sum of the above values.
Result :
proc | subscriber | col3
-----|------------|------------
0.7 | 123456 | 0.7
0.5 | 1234567 | 1.2 -- 0.7 + proc
0.3 | 12345 | 1.5
...
I found the following method:
Select a.[proc],SUM(b.[proc])
from tbl_proc a, tbl_proc b
where a.[proc] <= b.[proc] and (a.[proc] <> b.[proc] or a.subscriber >= b.subscriber)
group by a.[proc],a.subscriber
order by a.[proc] desc
In my table the data is sorted desc by proc. Also the subscriber column is unique.
This method I found is a little bit too expensive ( my tables are large ). Due to performance reasons I did not considered th cursor - like solution.
Any suggestions?
Update:
I googled the problem a little bit more and I found the "Update to a local variable" solution on this page:
As far as I tested this proves to be the best solution so far.
declare @runningTotal float = 0
UPDATE tbl_proc SET @RunningTotal = new_col = @RunningTotal + [proc] FROM tbl_proc