I may need help with a SQL statement. So I have a table called "cont" that looks like this:
cont_id name weight ----------- ---------- ----------- 1 1 10 2 1 20 3 2 40 4 2 15 5 2 20 6 3 15 7 3 40 8 4 60 9 5 10 10 6 5
Then I summed up the weight column and grouped it by name:
name wsum ---------- ----------- 2 75 4 60 3 55 1 30 5 10 6 5
And the result should have an accumulated column and should look like this:
name wsum acc_wsum ---------- ----------- ------------ 2 75 75 4 60 135 3 55 190 1 30 220 5 10 230 6 5 235
But I was not able to get the last expression to work.
edit: this expression did it (thanks to Gordon)
select t.*, (select sum(wsum) from (select name, SUM(weight) wsum from cont group by name) t2 where t2.wsum > t.wsum or (t2.wsum = t.wsum and t2.name <= t.name)) as acc_wsum from (select name, SUM(weight) wsum from cont group by name) t order by wsum desc
source share