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
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
I have bulit a basic query, you can work around it and get what you want.
select greatest(Start_time, trunc(Start_time+(level-1)/24, 'hh24')),
least(End_time, trunc(Start_time+(level)/24, 'hh24'))
from log_table
connect by level <= floor((dt1-dt2)*24)+1;
Example at sqlfiddle:
http://sqlfiddle.com/#!4/82625/29