ID AccountID Quantity 1 1 10 Sum = 10 2 1 5 = 10 + 5 = 15 3 1 2 = 10 + 5 + 2 = 17 4 2 7 = 7 5 2 3 = 7 + 3 = 10 SELECT ID,AccountID,Quantity,SUM(Quantity) OVER (PARTITION BY AccountID ) AS TopBorcT,FROM tCariH
要使用SUM()OVER()获取运行总数,您需要在PARTITION BY …之后添加一个ORDER BY子子句,如下所示:
SUM(Quantity) OVER (PARTITION BY AccountID ORDER BY ID)
但请记住,并不是所有的数据库系统都支持ORDER BY在窗口聚合函数的OVER子句中. (例如,sql Server直到最新版本sql Server 2012才支持它).