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

时光总嘲笑我的痴心妄想 提交于 2020-01-02 08:32:35

问题


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 :

Is There anyway to fix this?


回答1:


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.



来源:https://stackoverflow.com/questions/34428444/running-total-using-windows-function-in-sql-has-same-result-for-same-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!