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

如何使用lag函数查找sql server中相同列的连续行之间的差异,sql,sql-server,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

我一直在尝试下面的查询,但我得到了恒定的差异

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
你能这样做吗?:

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