Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql progressive sum

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:

http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx

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

like image 1000
Corovei Andrei Avatar asked Sep 06 '11 14:09

Corovei Andrei


Video Answer


1 Answers

This is generally known as calculating running totals.

There is a very fast method to do what you want to do called "quirky update" but it relies on undocumented behaviour.

Other than that cursors are the fastest way for large sets as the workload for these grows linearly whereas your triangular join workload grows exponentially (until next version and the improved OVER clause).

See this document by Itzik Ben Gan for more about the issue.

like image 75
Martin Smith Avatar answered Nov 15 '22 05:11

Martin Smith