SQL Oracle Counting Clusters

前端 未结 4 876
青春惊慌失措
青春惊慌失措 2020-12-20 23:44

I have a data set which is based on a timestamp.

     Date                 Value
07-Jul-15 12:05:00          1  
07-Jul-15 12:10:00          1 
07-Jul-15 12:         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-21 00:34

    Hi i have tried with below code it's displaying results as expected

    with cte
        as
        (
        select Rownum rn,a.* from table a 
        )
        select date1 as start_time,endtime,  (endtime-date1)*24*60 as period 
        from 
        (
        select date1,value,case 
        when 
        value=1 then
        lead(date1) over (order by date1)
        else null
        end
         as endtime
        from
        (
        select date1,value from cte where cte.rn=1
        union all
        select a.date1,a.value from cte a
        join
        cte b
        on 
        a.rn=b.rn+1
        and( (a.value=1 and b.value=0) or (a.value=0 and b.value=1)) 
        ) order by date1) where value=1
    

提交回复
热议问题