Oracle, split a time duration row by one hour period

前端 未结 2 889
面向向阳花
面向向阳花 2020-12-11 10:19

I\'m working on oracle db and I\'m not quite good at Oracle. I\'m trying to split a row by one hour period.

For example, if a time row has given as below,

St

2条回答
  •  爱一瞬间的悲伤
    2020-12-11 11:00

    Slightly modified version of the query by . The earlier query did not work in some cases say from date/time is 2:37 am and to date is 6:10 am.

    WITH date1 AS (
      SELECT to_date('2017-07-26 02:37:00 AM','yyyy-MM-dd hh12:mi:ss am') dt2,
             to_date('2017-07-26 06:10:00 PM','yyyy-MM-dd hh12:mi:ss am') dt1 FROM DUAL
    )
    select TO_CHAR (greatest(dt2, trunc(dt2+(level-1)/24, 'hh24')),
        'dd-MON-yyyy HH12:MI:SS AM') FROM_DT,
        TO_CHAR (  least(dt1, trunc(dt2+(level)/24, 'hh24'),
        'dd-MON-yyyy HH12:MI:SS AM') TO_DT
        from date1 connect by level <= floor(
            (trunc(dt1,'HH')-trunc(dt2,'HH'))  *24) + case when (
                 dt1 <> trunc(dt1,'HH')
            ) then 1 else 0 end
    

提交回复
热议问题