仅对最新行使用 LAG 函数
Use LAG Function on the latest row only
我有一个 table 看起来像这样:
这个table包含很多行。我想使用 LAG 函数来计算 cyclestart 和 getdate()
之间的日期差异以获得循环结束时间,在最新记录与先前记录的对比上,否则它会计算所有记录的 LAG,我不能在这么大的 table.
select
@AssetID,
@ProductionDay AS 'Production Date',
shift,
SKU,
CycleEnd AS 'Press Close Time',
@shift12,
cast( (CycleEnd - lag(CycleEnd) over (order by cycleend)) as datetime) as 'Cure Time'
from
Cycle
where
cycle.ID = @CycleID
如果我使用 TOP 1
或 TOP 2
,那么固化时间为 NULL。
LAG()
从前一行访问数据,而你正在 returning 1 行并且没有前一行,所以它总是 return null。
我有一个 table 看起来像这样:
这个table包含很多行。我想使用 LAG 函数来计算 cyclestart 和 getdate()
之间的日期差异以获得循环结束时间,在最新记录与先前记录的对比上,否则它会计算所有记录的 LAG,我不能在这么大的 table.
select
@AssetID,
@ProductionDay AS 'Production Date',
shift,
SKU,
CycleEnd AS 'Press Close Time',
@shift12,
cast( (CycleEnd - lag(CycleEnd) over (order by cycleend)) as datetime) as 'Cure Time'
from
Cycle
where
cycle.ID = @CycleID
如果我使用 TOP 1
或 TOP 2
,那么固化时间为 NULL。
LAG()
从前一行访问数据,而你正在 returning 1 行并且没有前一行,所以它总是 return null。