Whitout 对每一行都有任何主键或一些不同的 id ......我想到的唯一解决方案是:
select rn, id , time
from
(select ROW_NUMBER() over (order by time) as rn, id, time from test
where id = 'start'
union
select ROW_NUMBER() over (order by time) as rn, id, time from test
where id = 'stop'
) d
order by rn
基本上我在开始行和停止行之间建立了一个联合,如下所示:
(select ROW_NUMBER() over (order by time) as rn, id, time from test
where id = 'start'
union
select ROW_NUMBER() over (order by time) as rn, id, time from test
where id = 'stop'
) d
返回:
1 start 08.00
2 start 11.00
3 start 12.00
4 start 13.00
1 stop 09.00
2 stop 10.00
3 stop 14.00
4 stop 15.00
从原始输入:
id time
start 08.00
stop 09.00
stop 10.00
start 11.00
start 12.00
start 13.00
stop 14.00
stop 15.00
现在您只需按他们自己的行号对它们进行排序......就是那个 rn。
最后,您将拥有:
1 start 08.00
1 stop 09.00
2 start 11.00
2 stop 10.00
3 start 12.00
3 stop 14.00
4 start 13.00
4 stop 15.00
@注意:我的示例值与您的值接近...但是虚构的...