如何从结果集中获取最后一行的值,并将其与结果集中的下一组行相加 [英] how to get the last row value from resultset and add it with next set of rows in the resultset
问题描述
大家好,
我如何在sql server中实现以下逻辑:
我在表中有3列说:
Hi all,
How can i implement this following logic in sql server :
I have 3 columns in a table say :
Distance Height Sequence
0.91 10 1
0.98 11 1
1.2 12 1
0 13 2
0.81 15 2
0.98 17 2
1.6 19 3
2.1 19.5 3
逻辑如下:
我应该将偏移值设置为0.0
当新序列开始时,我应该在先前的行序列值上加上偏移值
例如:
设置offset = 0.0
偏移量= 0.0 + 1.2(上一行值)= 1.2
我应该为序列号为2的值添加上述偏移值.
具有2的序列的输出将是:
logic is as follows:
i should set offset value as 0.0
when new sequence starts, i should add offset value with previous row sequence value
eg:
set offset=0.0
offset=0.0+1.2(previous row value)=1.2
i should add the above offset value for values having sequence numbers as 2.
output for sequence having 2 will be :
Distance Height Sequence
1.2 13 2
2.01 15 2
2.18 17 2
现在offset = 1.2 + 2.18(上一行值)= 3.38
以上偏移值,我应该添加序列号为3
now offset=1.2+2.18(previous row value)=3.38
above offset value, i should add for sequence numbers as 3
Distance Height Sequence
4.98 19 3
5.48 19.5 3
总的来说,我的表应该返回以下输出:
Overall my table should return the following output :
Distance Height Sequence
0.91 10 1
0.98 11 1
1.2 12 1
1.2 13 2
2.01 15 2
2.18 17 2
4.98 19 3
5.48 19.5 3
在此先谢谢您.
Thanks in advance.
推荐答案
您是否正在寻找递归CTE?
http://msdn.microsoft.com/zh-cn/library/ms186243(v = sql.105).aspx
are you looking for a recursive CTE?
http://msdn.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
这篇关于如何从结果集中获取最后一行的值,并将其与结果集中的下一组行相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!