一张表的原始数据,数据没有排序A B C (开始时间) D (结束时间) E (上一条的结束时间) F(时间间隔 C-E)
320230 32023010717531_06 2007-04-28 14:12:19 2007-04-28 15:41:50
320220 32022010717531_07 2007-04-28 15:42:36 2007-04-28 16:34:20
301310 30131010717531_08 2007-04-28 16:47:35 2007-04-28 17:21:33
101310 10131010717531_09 2007-04-28 17:22:00 2007-04-28 18:19:56
106220 10622010717531_10 2007-04-28 18:20:26 2007-04-28 19:42:37
110230 11023010717531_11 2007-04-28 19:43:23 2007-04-28 23:46:15
116230 11623010717531_12 2007-04-28 23:50:26 2007-04-29 01:08:53 要求生成以下结果,如何实现?最好用一条select实现320230 32023010717531_06 2007-04-28 14:12:19 2007-04-28 15:41:50
320220 32022010717531_07 2007-04-28 15:42:36 2007-04-28 16:34:20 2007-04-28 15:41:50 46
301310 30131010717531_08 2007-04-28 16:47:35 2007-04-28 17:21:33 2007-04-28 16:34:20 795
101310 10131010717531_09 2007-04-28 17:22:00 2007-04-28 18:19:56 2007-04-28 17:21:33 27
106220 10622010717531_10 2007-04-28 18:20:26 2007-04-28 19:42:37 2007-04-28 18:19:56 30
110230 11023010717531_11 2007-04-28 19:43:23 2007-04-28 23:46:15 2007-04-28 19:42:37 46
116230 11623010717531_12 2007-04-28 23:50:26 2007-04-29 01:08:53 2007-04-28 23:46:15 251

解决方案 »

  1.   

    按照开始时间排序后
    那会不会有上条结束时间<本条开始时间select t.a,t.b,t.c,t.d,t2.d as e,t2.d-t.d as f   from (select A,B,C,D,rownum as rn from table
    order by C) t,(select a,b,c,d,rownum+1 as rn2 from table ) t2
    where t.rn=t2.rn2差不多是这样的
      

  2.   

    select a,b,c,d,first_value(d) over (order by arows between 1 preceding and 0 following) e,d-c f from tab;