我正在使用 SQL Server 2008,如何计算开启和关闭状态之间的时间?我有下表(按顺序排列timestamp
):
ID | EQUIP_ID | TIMESTAMP | STATUS (1 on/0 off)
1 | 1 | 21/05/2012 13:00:00 | 1
3 | 1 | 21/05/2012 13:04:00 | 1
4 | 1 | 21/05/2012 13:05:00 | 0
6 | 1 | 21/05/2012 13:09:00 | 1
7 | 1 | 21/05/2012 13:10:00 | 1
9 | 1 | 21/05/2012 13:12:00 | 1
10 | 1 | 21/05/2012 13:13:00 | 0
10 | 1 | 21/05/2012 13:14:00 | 1
10 | 1 | 21/05/2012 13:15:00 | 0
我期待这样的结果:
EQUIP_ID | START | END | STATUS
1 | 21/05/2012 13:00:00 | 21/05/2012 13:05:00 | 1 (WORKING)
1 | 21/05/2012 13:05:00 | 21/05/2012 13:09:00 | 0 (STOPPED)
1 | 21/05/2012 13:09:00 | 21/05/2012 13:13:00 | 1
1 | 21/05/2012 13:13:00 | 21/05/2012 13:14:00 | 0
1 | 21/05/2012 13:14:00 | 21/05/2012 13:15:00 | 1
我已经尝试了一些间隙和岛屿的功能,但没有奏效,我不知道我错过了什么......