Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running total using windows function in sql has same result for same data

From every references that I search how to do cumulative sum / running total. they said it's better using windows function, so I did

select grandtotal,sum(grandtotal)over(order by agentname) from call

but I realize that the results are okay as long as the value of each rows are different. Here is the result :

result

Is There anyway to fix this?

like image 684
Elbert Avatar asked Apr 13 '26 11:04

Elbert


1 Answers

You might want to review the documentation on window specifications (which is here). The default is "range between" which defines the range by the values in the row. You want "rows between":

select grandtotal,
       sum(grandtotal) over (order by agentname rows between unbounded preceding and current row)
from call;

Alternatively, you could include an id column in the sort to guarantee uniqueness and not have to deal with the issue of equal key values.

like image 177
Gordon Linoff Avatar answered Apr 16 '26 04:04

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!