E_status  状态有很多种 如:  STOPS, RUN,DOWN,SETUP,DEA
e_id  机台编号 A1,A2
l_id    线别 A ,B ,一条线可以有多个机台 
start_time    开始时间,每次切换状态的时间 要得到每次距离RUN最近的一笔的时间,如果没有距离最近的时间,则默认今天的最大时间 
l_id      e_id      E_status          start_time                
A            A1        DOWN        2000-11-11  10:00:00      
A            A1        RUN        2000-11-11  11:00:00    
A            A1        DOWN        2000-11-11  12:00:00 
A            A1      SETUP        2000-11-11  12:30:00        
A            A1      DOWN        2000-11-11  13:00:00    
A            A1        RUN        2000-11-11  15:00:00      
A            A1      DOWN        2000-11-11  16:00:00      
A            A2      DOWN        2000-11-11  10:00:00    
A            A2        RUN        2000-11-11  11:00:00    
A            A2        RUN        2000-11-11  12:00:00      
A            A2        RUN        2000-11-11  13:00:00    
A            A2        DOWN        2000-11-11  14:00:00 
A            A2        RUN        2000-11-11  18:00:00
 得到如下结果。
 l_id      e_id      E_status     start_time                     nest_time
 A        A1        RUN        2000-11-11  11:00:00   2000-11-11  12:00:00
 A        A1        RUN       2000-11-11  15:00:00    2000-11-11  16:00:00
 A        A2        RUN        2000-11-11  11:00:00   2000-11-11  12:00:00
 A        A2        RUN        2000-11-11  12:00:00   2000-11-11  13:00:00
 A        A2        RUN        2000-11-11  13:00:00   2000-11-11  14:00:00
 A       A2        RUN        2000-11-11  18:00:00    2000-11-11  23:59:95

解决方案 »

  1.   

    存储过程容易,sql。。不会啊。。
    关注,up
      

  2.   

    select * from(
      select tt.*,nvl(lead(start_time)over(partition by l_id,e_id order by start_time),sysdate)next_time
      from tt)
    where e_status='RUN'
      

  3.   

    或者
    select a.*,nvl(b.start_time,sysdate)next_time
    from tt a,tt b
    where a.e_status='RUN'
      and a.l_id=b.l_id(+)
      and a.e_id=b.e_id(+)
      and a.start_Time<b.start_time(+)
      and not exists(select 1 from tt where l_id=b.l_id and e_id=b.e_id and start_time<b.start_time and start_time>a.start_time)
    order by 1,2,5效率应该没有上面的高