Lead()考虑where子句,不要它

时间:2014-08-07 12:30:28

标签: tsql sql-server-2012

我有一个名为TaskLog的简单日志表。我想通过查看该步骤与之后的时间差来看看某个步骤需要多长时间。

下面的查询具有查看读取'执行媒体关系累加的行之间的时间的效果。 - 我不希望LEAD()行遵循WHERE子句。 (无论Notes的值如何,它们都应该在cteTask中占据下一行。

;with cteTask as
    (select * from Tasklog where prog = 'StatsMajor' and Date>'8/1/2014')
, cteLead as
    (select *
        , LEAD(ID) over (order by ID) NextID
        , LEAD(date) over (order by ID) NextDt

    from cteTask 
    where notes = 'performing media relations rollup'
    )
select *, DATEDIFF(second, Date, NextDt) as Secs
from cteLead

1 个答案:

答案 0 :(得分:1)

只是过滤后?

;with cteTask as
    (select * from Tasklog where prog = 'StatsMajor' and Date>'8/1/2014')
, cteLead as
    (select *
        , LEAD(ID) over (order by ID) NextID
        , LEAD(date) over (order by ID) NextDt

    from cteTask 
    )
select *, DATEDIFF(second, Date, NextDt) as Secs
from cteLead
where notes = 'performing media relations rollup'