本文介绍了从上一行减去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,我正在使用Sql server 2012。我有一张桌子
ie
Column1
32
-153
174
-5
我想要结果表格像
Hello guys,I am using Sql server 2012 . I have a table
i.e
Column1
32
-153
174
-5
I want result table like
Column1 Column2
32 32
-153 -121
174 53
-5 48
...
in Each Row下一行的值加上prev值
ie -153 + 32 = -121
-121 + 174 = 53
我听说Sql server 2012中的Lead和Lag但尚未使用。如果您有任何想法那么请告诉我..
...
in Each Row next row value is added with prev value
i.e -153 + 32 =-121
-121+174=53
I heard about Lead and Lag in Sql server 2012 but not yet used.If you have any idea then let me know..
推荐答案
WITH cte1 AS
(
select column1,
rownum = row_number() OVER (ORDER BY (SELECT 0))
from table1
)
SELECT column1,
case rownum
when 1 then column1 else
column1 +
(
select sum(column1) from cte1 c2 where c2.rownum < c1.rownum
)
end
FROM cte1 c1
WHERE rownum > 0
这篇关于从上一行减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!