如何求取累积和declare @t table ( id int, SomeNumt int )insert into @tselect 1,10unionselect 2,12unionselect 3,3unionselect 4,15unionselect 5,23select * from @t上面的选择返回以下内容。id SomeNumt1 102 123 34 155 23如何获得以下信息id srome CumSrome1 10 102 12 223 3 254 15 405 23 63
3 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
SQLServer(2012)的最新版本允许以下操作。
SELECT RowID, Col1, SUM(Col1) OVER(ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2FROM tablehhORDER BY RowId
或
SELECT GroupID, RowID, Col1, SUM(Col1) OVER(PARTITION BY GroupID ORDER BY RowId ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Col2FROM tablehhORDER BY RowId
这更快。分区版本在34秒内完成,超过500万行。
添加回答
举报
0/150
提交
取消