在SQL中使用Windows函数运行总计对相同数据的结果相同 [英] running total using windows function in sql has same result for same data

查看:93
本文介绍了在SQL中使用Windows函数运行总计对相同数据的结果相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我搜索的每个参考中,如何做累计和/累计。他们说使用Windows函数更好,所以我做到了

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?

推荐答案

您可能想查看有关窗口规范的文档(即此处)。默认值为范围之间,它通过行中的值定义范围。您希望行之间:

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;

或者,您可以包含 id 列可以保证唯一性,而不必处理键值相等的问题。

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.

这篇关于在SQL中使用Windows函数运行总计对相同数据的结果相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆