如何使用滞后函数找到sql server中相同列的连续行之间的差异

时间:2017-06-15 07:39:56

标签: sql sql-server

尝试使用以下查询,但我得到了不变的差异

with cte as 
(
select businessentityid,nationalidnumber,SickLeaveHours,LAG(sickleavehours) over (order by businessentityid) as PreviousRow
from HumanResources.Employee
)
select *,DIFFERENCE(sickleavehours,cte.previousrow) from cte

enter image description here

1 个答案:

答案 0 :(得分:0)

你能这样做吗?:

with cte as 
(
select businessentityid,nationalidnumber,SickLeaveHours,LAG(sickleavehours) over (order by businessentityid) as PreviousRow
from HumanResources.Employee
)
select *, sickleavehours - cte.previousrow as difference from cte