嗨,我在表格Bills中有一列名为Qty的列
我想要一列显示这样的Qty列的运行总和:

Qty   Run_Sum
1      1
2      3
3      6
4      10
5      15


建议我一些合适的方法来运行一些感谢

最佳答案

如果RDBMS支持窗口功能,



对于SQL Server 2012

SELECT  Qty,
        SUM(Qty) OVER (ORDER BY Qty) AS CumulativeTOTAL
FROM    tableName



SQLFiddle Demo




对于SQL Server 2008

SELECT a.Qty, (SELECT SUM(b.Qty)
               FROM   TableName b
               WHERE  b.Qty <= a.Qty)
FROM   TableName a
ORDER  BY a.Qty;



SQLFiddle Demo

关于sql - 如何在SQL Server中获取列的运行总和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14953294/

10-13 00:04