您可以使用PIVOT函数获取结果,我只需将
row_number()windowing函数应用于数据,以便可以为每个返回多个行
ID2:
select id2, start, stopfrom( select id2, status, time, row_number() over(partition by statusorder by time) seq from yourtable) dpivot( max(time) for status in (start, stop)) pivorder by start desc;
请参阅带有演示的SQL Fiddle。
您还可以将聚合函数与CASE表达式一起使用以获取最终结果:
select id2, max(case when status = 'start' then time end) start, max(case when status = 'start' then time end) stopfrom ( select id2, status, time, row_number() over(partition by statusorder by time) seq from yourtable) dgroup by id2, seq;
参见带有演示的SQL Fiddle
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)