有如下的表格:
ModifyDate| ModifiedBy | TaskID |
2018-02-05| Bob | 55444 |
2018-02-06| Lily | 55444 |
2018-02-08| Sarah | 55444 |
对于每一行,我需要计算每个TaskID在Modifydate之间的时间差(以天为单位)。
例如,我的输出应如下所示:
ModifyDate| ModifiedBy | TaskID | Time
2018-02-05| Bob | 55444 | 1
2018-02-06| Lily | 55444 | 2
2018-02-08| Sarah | 55444 | NULL
SELECT ModifyDate, ModifiedBy, TaskID,
DATEDIFF(DAY, ModifyDate,
LEAD(ModifyDate) OVER (PARTITION BY TaskID
ORDER BY ModifyDate))
FROM mytable