我想在sequencel行的差值之间进行计算。例如,如果存在rn和一个低rn(行号4和5),则将对其进行计算。
你能给我任何想法吗?
答案 0 :(得分:1)
您可以使用CTE(假设SQL 2005+)和ROW_NUMBER()执行此操作:
;With Rows
As
(
Select *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) As RowNum
From Lag
)
Select R1.LagValue, R1.Value, R1.rn, R1.rn - IsNull(R2.rn, 0) As Difference
From Rows R1
Left Join Rows R2 On R1.RowNum = R2.RowNum + 1
答案 1 :(得分:0)
看看LAG。它是这样的:
select [value] - lag([value], 1, 0)
over (partition by [LagValue] order by [rn] desc)
from your_table