sql查询来计算两个连续行之间的时间差

时间:2016-09-29 16:37:01

标签: sql-server

hh:mm从01更改为另一行Procs_ID时,任何人都可以帮我计算两行之间的时差(Procs_ID}。

Procs_ID  meter_id        date
  01      0000012         2015-10-12 09:07:22.530
  03      0000013         2015-10-12 09:11:51.733
  01      0000014         2015-10-12 09:12:38.550
  02      0000015         2015-10-12 10:38:52.923
  03      0000016         2015-10-12 10:40:33.467
  01      0000017         2015-10-12 10:40:56.013

提前致谢

1 个答案:

答案 0 :(得分:0)

以下是LEAD的示例,可用于SQL Server 2012 +

select
    *,
    case 
        when Procs_Id = '01' 
             and lead(Procs_ID) over (order by [date]) <> Procs_ID then datediff(mi,[date],lead([date]) over (order by [date])) 
    end as TimeDiffInMin
from YourTable