Group by Hour for Epoch time - sql

I need to group by Hour for converted epoch time, here is what I have done but it is still grouping by the day. Can anyone support?
select count (*) as "Total",to_char(trunc(to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME),'DD/MM/YYYY HH24') as "Day"
from Records
where ID=35 and (INSERTION_TIME/86400)+ to_date('01-01-1970 00:00:00','dd-mm-yyyy HH24:MI:SS') >=sysdate - 4
group by trunc(to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME)
order by trunc(to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME)

You can get rid of most of the arithmetic and just use:
SELECT COUNT(*) as "Total",
TO_CHAR(
TRUNC(DATE '1970-01-01' + INTERVAL '1' SECOND * INSERTION_TIME, 'HH'),
'DD/MM/YYYY HH24'
) as "Day"
FROM Records
WHERE ID=35
AND DATE '1970-01-01' + INTERVAL '1' SECOND * INSERTION_TIME >=sysdate - 4
GROUP BY
TRUNC(DATE '1970-01-01' + INTERVAL '1' SECOND * INSERTION_TIME, 'HH')
ORDER BY
TRUNC(DATE '1970-01-01' + INTERVAL '1' SECOND * INSERTION_TIME, 'HH')
Note: Epoch time is usually 1970-01-01 00:00:00 UTC. If your session time zone is not UTC then you may be calculating the epoch time incorrectly and would want to use timestamps instead:
SELECT COUNT(*) as "Total",
TO_CHAR(
TRUNC(
TIMESTAMP '1970-01-01 00:00:00 UTC' + INTERVAL '1' SECOND * INSERTION_TIME,
'HH'
),
'DD/MM/YYYY HH24'
) as "Day"
FROM Records
WHERE ID=35
AND TIMESTAMP '1970-01-01 00:00:00 UTC' + INTERVAL '1' SECOND * INSERTION_TIME
>=SYSTIMESTAMP - INTERVAL '4' DAY
GROUP BY
TRUNC(
TIMESTAMP '1970-01-01 00:00:00 UTC' + INTERVAL '1' SECOND * INSERTION_TIME,
'HH'
)
ORDER BY
TRUNC(
TIMESTAMP '1970-01-01 00:00:00 UTC' + INTERVAL '1' SECOND * INSERTION_TIME,
'HH'
)

Notice the behavior of trunc on your expression. It removes the hour component.
Test case with dbfiddle.uk:
Test case
The ungrouped SQL, with extra expression in select list:
select to_char(trunc(to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME),'DD/MM/YYYY HH24') as "Day"
, to_char( (to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME),'DD/MM/YYYY HH24') as "Day"
from Records
where ID = 35 and (INSERTION_TIME/86400) + to_date('01-01-1970 00:00:00','dd-mm-yyyy HH24:MI:SS') >= sysdate - 4
AND rownum < 6
order by (to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME)
;
The ungrouped result:
Now add the GROUP BY:
select COUNT(*) AS n
, to_char(trunc(to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME),'DD/MM/YYYY HH24') as "Day"
, to_char( (to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME),'DD/MM/YYYY HH24') as "Day"
from Records
where ID = 35 and (INSERTION_TIME/86400) + to_date('01-01-1970 00:00:00','dd-mm-yyyy HH24:MI:SS') >= sysdate - 4
group by (to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME)
order by (to_date('19700101 00', 'YYYYMMDD HH24') + ( 1 / 24 / 60 / 60 ) * INSERTION_TIME)
;
The grouped result:
To create a test case:
CREATE TABLE Records (
id int
, insertion_time int
);
INSERT INTO Records VALUES (35, 4011134567);
INSERT INTO Records VALUES (35, 4011134567);
INSERT INTO Records VALUES (35, 4011134567);
INSERT INTO Records VALUES (35, 4011134567);

Related

calculate column names automatically sql

Is there a way to calculate column names automatically in SQL like below. I need top calculate the Calendar weeks based on from and to date and distribute evenly
Material
From
To
Sales
M01
03.10.2022
31.10.2022
1000
M02
14.11.2022
28.11.2022
1000
Expected output
CW =calendar week
Material
Cw40
CW41
Cw42
CW43
CW44
CW45
CW46
CW47
M01
250
250
250
250
M02
500
500
Is there a way to calculate column names automatically in SQL like below.
No, in SQL (not just Oracle SQL) you needs a fixed, known number of column names so it is impossible to dynamically generate columns with a static SQL query.
If you want to generate the data then either:
Generate the data as rows (rather than columns) and pivot the result in whatever third-party application you are using to access the database. You can generate the output using a correlated row-generator:
SELECT t.material,
w.iso_year,
w.iso_week,
w.weekly_sales
FROM table_name t
CROSS APPLY (
SELECT TO_NUMBER(
TO_CHAR(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL - 1),
'IYYY'
)
) AS iso_year,
TO_NUMBER(
TO_CHAR(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL - 1),
'IW'
)
) AS iso_week,
( LEAST(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * LEVEL,
to_dt
)
- GREATEST(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL - 1),
from_dt
)
) / (to_dt - from_dt) * sales AS weekly_sales
FROM DUAL
CONNECT BY TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL-1) < to_dt
) w
or:
WITH data (from_dt, dt, to_dt, material, sales) AS (
SELECT from_dt, from_dt, to_dt, material, sales
FROM table_name
UNION ALL
SELECT from_dt,
TRUNC(dt + INTERVAL '7' DAY, 'IW'),
to_dt,
material,
sales
FROM data
WHERE TRUNC(dt + INTERVAL '7' DAY, 'IW') < to_dt
)
SELECT material,
TO_NUMBER(TO_CHAR(dt, 'IYYY')) AS iso_year,
TO_NUMBER(TO_CHAR(dt, 'IW')) AS iso_week,
( LEAST(dt + INTERVAL '7' DAY, to_dt) - dt)
/ (to_dt - from_dt) * sales AS weekly_sales
FROM data
Which, for the sample data:
CREATE TABLE table_name (Material, From_dt, To_dt, Sales) AS
SELECT 'M01', DATE '2022-10-03', DATE '2022-10-31', 1000 FROM DUAL UNION ALL
SELECT 'M02', DATE '2022-11-14', DATE '2022-11-28', 1000 FROM DUAL;
Both output:
MATERIAL
ISO_YEAR
ISO_WEEK
WEEKLY_SALES
M01
2022
40
250
M01
2022
41
250
M01
2022
42
250
M01
2022
43
250
M02
2022
46
500
M02
2022
47
500
Or, if you did want to output the values as columns then you need to specify the columns (which would be 53 columns for all 53 potential ISO weeks) and can do that using:
SELECT *
FROM (
SELECT t.material,
w.iso_year,
w.iso_week,
w.weekly_sales
FROM table_name t
CROSS APPLY (
SELECT TO_NUMBER(
TO_CHAR(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL - 1),
'IYYY'
)
) AS iso_year,
TO_NUMBER(
TO_CHAR(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL - 1),
'IW'
)
) AS iso_week,
( LEAST(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * LEVEL,
to_dt
)
- GREATEST(
TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL - 1),
from_dt
)
) / (to_dt - from_dt) * sales AS weekly_sales
FROM DUAL
CONNECT BY TRUNC(from_dt, 'IW') + INTERVAL '7' DAY * (LEVEL-1) < to_dt
) w
)
PIVOT (
SUM(weekly_sales)
FOR iso_week IN (
1 AS cw01,
2 AS cw02,
3 AS cw03,
-- ...
40 AS cw40,
41 AS cw41,
42 AS cw42,
43 AS cw43,
44 AS cw44,
45 AS cw45,
46 AS cw46,
47 AS cw47,
48 AS cw48,
49 AS cw49,
50 AS cw50,
51 AS cw51,
52 AS cw52,
53 AS cw53
)
)
or:
WITH data (from_dt, dt, to_dt, material, sales) AS (
SELECT from_dt, from_dt, to_dt, material, sales
FROM table_name
UNION ALL
SELECT from_dt,
TRUNC(dt + INTERVAL '7' DAY, 'IW'),
to_dt,
material,
sales
FROM data
WHERE TRUNC(dt + INTERVAL '7' DAY, 'IW') < to_dt
)
SELECT *
FROM (
SELECT material,
TO_NUMBER(TO_CHAR(dt, 'IYYY')) AS iso_year,
TO_NUMBER(TO_CHAR(dt, 'IW')) AS iso_week,
( LEAST(dt + INTERVAL '7' DAY, to_dt) - dt)
/ (to_dt - from_dt) * sales AS weekly_sales
FROM data
)
PIVOT (
SUM(weekly_sales)
FOR iso_week IN (
1 AS cw01,
2 AS cw02,
3 AS cw03,
-- ...
40 AS cw40,
41 AS cw41,
42 AS cw42,
43 AS cw43,
44 AS cw44,
45 AS cw45,
46 AS cw46,
47 AS cw47,
48 AS cw48,
49 AS cw49,
50 AS cw50,
51 AS cw51,
52 AS cw52,
53 AS cw53
)
)
Which both output:
MATERIAL
ISO_YEAR
CW01
CW02
CW03
CW40
CW41
CW42
CW43
CW44
CW45
CW46
CW47
CW48
CW49
CW50
CW51
CW52
CW53
M01
2022
null
null
null
250
250
250
250
null
null
null
null
null
null
null
null
null
null
M02
2022
null
null
null
null
null
null
null
null
null
500
500
null
null
null
null
null
null
fiddle

Oracle query Round up or down to nearest 15 minute interval

08-SEP-20 08:55:05
08-SEP-20 15:36:13
The query below is working correctly for 15:36:13 in that it rounds to 15:30 but the 8:55:05 is rounding down to 08:45 when it should be rounding to 09:00
select event_date,trunc(event_date,'mi') - numtodsinterval( mod(to_char(event_date,'mi'),15), 'minute' ) as nearest_quarter
from time_source_in_all where empno = '002307718' and event_date between '8-sep-2020' and '9-sep-2020'
I think this will do what you want:
select trunc(event_date, 'HH') + round(extract(minute from event_date) / 15) * interval '15' minute )
. . .
Note: I prefer the extract() because it is standard SQL. But it assumes that the column is a timestamp and not a date.
or the equivalent:
select trunc(event_date, 'HH') + numtodsinterval(round(to_number(to_char(event_date, 'MI')) / 15) * 15, 'minute')
You can use:
SELECT event_date,
TRUNC( event_date, 'HH' )
+ ROUND( EXTRACT( MINUTE FROM CAST( event_date AS TIMESTAMP ) ) / 15 )
* INTERVAL '15' MINUTE
AS rounded_15_event_date
FROM table_name
or:
SELECT event_date,
TRUNC( event_date, 'HH' )
+ ROUND( ( event_date - TRUNC( event_date, 'HH' ) ) * 24 * 4 )
* INTERVAL '15' MINUTE
AS rounded_15_event_date
FROM table_name
Which, for your sample data:
CREATE TABLE table_name ( event_date ) AS
SELECT DATE '2020-09-08' + INTERVAL '08:55:05' HOUR TO SECOND FROM DUAL UNION ALL
SELECT DATE '2020-09-08' + INTERVAL '15:36:13' HOUR TO SECOND FROM DUAL
Both output:
EVENT_DATE | ROUNDED_15_EVENT_DATE
:------------------ | :--------------------
2020-09-08 08:55:05 | 2020-09-08 09:00:00
2020-09-08 15:36:13 | 2020-09-08 15:30:00
db<>fiddle here

Multiple row to multiple column based on data in SQL Oracle

output will be as image
SQL Oracle query Sample
select * from (
select * from (
with fifteen as (select trunc(sysdate) + (level * 15)/(24*60) c_time from dual connect by level <= (24*60) / 15 )
select to_number(to_char(c_time, 'hh')) HR,to_char(c_time, 'hh24:mi')||' - '||to_char(c_time+ 15 / (24 * 60), 'hh24:mi') TimeSlots
from fifteen
where extract(hour from cast (c_time as timestamp)) between 2 and 5
) t
)
Oracle Query 1:
with fifteen as (
select CAST( TRUNC( sysdate ) AS TIMESTAMP ) + level * INTERVAL '15' MINUTE c_time,
MOD( LEVEL,4 ) AS quarter
from dual
connect by level <= (24*60) / 15
)
SELECT "2", "3", "4", "5"
FROM (
select EXTRACT( HOUR FROM c_time ) HR,
quarter,
to_char(c_time, 'hh24:mi')||' - '||to_char(c_time + INTERVAL '15' MINUTE, 'hh24:mi') TimeSlots
from fifteen
) t
PIVOT( MAX( timeslots ) FOR HR IN ( 2, 3, 4, 5 ) )
ORDER BY quarter
Oracle Query 2:
SELECT TO_CHAR(two, 'HH24:MI') || ' - ' || TO_CHAR(two +INTERVAL '15' MINUTE,'HH24:MI') AS "2",
TO_CHAR(three,'HH24:MI') || ' - ' || TO_CHAR(three+INTERVAL '15' MINUTE,'HH24:MI') AS "3",
TO_CHAR(four ,'HH24:MI') || ' - ' || TO_CHAR(four +INTERVAL '15' MINUTE,'HH24:MI') AS "4",
TO_CHAR(five ,'HH24:MI') || ' - ' || TO_CHAR(five +INTERVAL '15' MINUTE,'HH24:MI') AS "5"
FROM (
SELECT DATE '1970-01-01' + INTERVAL '2' HOUR + INTERVAL '15' MINUTE * (LEVEL - 1) AS two,
DATE '1970-01-01' + INTERVAL '3' HOUR + INTERVAL '15' MINUTE * (LEVEL - 1) AS three,
DATE '1970-01-01' + INTERVAL '4' HOUR + INTERVAL '15' MINUTE * (LEVEL - 1) AS four,
DATE '1970-01-01' + INTERVAL '5' HOUR + INTERVAL '15' MINUTE * (LEVEL - 1) AS five
FROM DUAL
CONNECT BY LEVEL <= 4
)
Output:
2 3 4 5
------------- ------------- ------------- -------------
02:00 - 02:15 03:00 - 03:15 04:00 - 04:15 05:00 - 05:15
02:15 - 02:30 03:15 - 03:30 04:15 - 04:30 05:15 - 05:30
02:30 - 02:45 03:30 - 03:45 04:30 - 04:45 05:30 - 05:45
02:45 - 03:00 03:45 - 04:00 04:45 - 05:00 05:45 - 06:00

oracle - data aggregation with 15 min interval for 1 hour data from start time

I have to get data from Oracle Table in which I have one datefield called periodstarttime and I want to get only the data aggregated(aggregation must be done for 1hr data) at minutes 00th,15th,30th,45th in a day with input data in 5 minute intervals
For example, if in my table I have periodstarttime as
periodstarttime data
05/04/2017 1:00:00 10
05/04/2017 1:05:00 1
05/04/2017 1:10:00 2
05/04/2017 1:15:00 3
05/04/2017 1:20:00 4
05/04/2017 1:25:00 5
05/04/2017 1:30:00
and so on....
then I want my result to look like:
periodstarttime data with 1hr aggregation
05/04/2017 1:00:00 data with 1hr aggregation from 1.00 to 2.00
05/04/2017 1:15:00 data with 1hr aggregation from 1.15 to 2.15
05/04/2017 1:30:00 data with 1hr aggregation from 1.30 to 2.30
05/04/2017 1:45:00 data with 1hr aggregation from 1.45 to 2.45
You can use TRUNC() and CASE to truncate to the nearest 15 minute interval and then use an analytic function with a windowing clause to get the sum over a rolling window:
SELECT DISTINCT
periodstarttime,
SUM( data ) OVER ( ORDER BY periodstarttime
RANGE BETWEEN INTERVAL '0' MINUTE PRECEDING
AND INTERVAL '45' MINUTE FOLLOWING
)
AS hour_total,
'data with 1hr aggregation from '
|| TO_CHAR( periodstarttime, 'HH24:MI' )
|| ' to '
|| TO_CHAR( periodstarttime + INTERVAL '1' HOUR, 'HH24:MI' )
AS range
FROM (
SELECT CASE
WHEN periodstarttime < TRUNC( periodstarttime, 'HH24' ) + INTERVAL '15' MINUTE
THEN TRUNC( periodstarttime, 'HH24' )
WHEN periodstarttime < TRUNC( periodstarttime, 'HH24' ) + INTERVAL '30' MINUTE
THEN TRUNC( periodstarttime, 'HH24' ) + INTERVAL '15' MINUTE
WHEN periodstarttime < TRUNC( periodstarttime, 'HH24' ) + INTERVAL '45' MINUTE
THEN TRUNC( periodstarttime, 'HH24' ) + INTERVAL '30' MINUTE
ELSE TRUNC( periodstarttime, 'HH24' ) + INTERVAL '45' MINUTE
END AS periodstarttime,
data
FROM table_name
)
ORDER BY periodstarttime;
Here is one way of doing it:
WITH your_table AS (SELECT to_date('05/04/2017 01:00:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 10 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:05:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 20 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:10:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 30 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:15:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 40 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:20:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 50 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:25:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 60 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:30:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 70 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:40:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 80 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 01:50:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 90 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 02:00:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 100 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 02:10:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 110 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 02:20:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 120 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 02:30:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 130 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 02:40:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 140 DATA FROM dual UNION ALL
SELECT to_date('05/04/2017 03:00:00', 'dd/mm/yyyy hh24:mi:ss') periodstarttime, 150 DATA FROM dual)
-- End of setting up data in your_table; you would not need the above since you already have a table with data in it
-- See the below SQL for the main statement:
SELECT DISTINCT TRUNC(periodstarttime, 'hh') + FLOOR((TRUNC(periodstarttime, 'mi') - TRUNC(periodstarttime, 'hh'))/(15/1440))*15/1440 periodstarttime,
sum(DATA) OVER (ORDER BY TRUNC(periodstarttime, 'hh') + FLOOR((TRUNC(periodstarttime, 'mi') - TRUNC(periodstarttime, 'hh'))/(15/1440))*15/1440
RANGE BETWEEN 0 PRECEDING AND 60/1440 FOLLOWING) sum_over_next_hour
FROM your_table
ORDER BY periodstarttime;
PERIODSTARTTIME SUM_OVER_NEXT_HOUR
---------------- ------------------
05/04/2017 01:00 660
05/04/2017 01:15 850
05/04/2017 01:30 770
05/04/2017 01:45 690
05/04/2017 02:00 750
05/04/2017 02:15 540
05/04/2017 02:30 290
05/04/2017 03:00 150
This works by:
finding the period start times, which we do by finding the minutes of each periodstarttime, dividing it by 15 minutes, finding the floor of that and then multiplying that new number by 15 minutes. That separates the times into 15 minute groups.
Now we know the groups, we can do a cumulative sum. Because we want values across the hour, we need to do a range between the current period and the current period + 1 hour. N.B. This is inclusive, so that the 02:00 values will be included in the cumulative sum for the 01:00 period. If you don't want that, change the range so that it goes up to 59 minutes (which is fine, since our periods are now 0, 15, 30 or 45 minutes past the hour).
Have a look at Windowing_clause of Analytic Functions.
Would be like this
select periodstarttime,
SUM(DATA) OVER (RANGE BETWEEN CURRENT ROW AND INTERVAL '1' HOUR FOLLOWING ORDER BY periodstarttime),
'data with 1hr aggregation from '||FIRST_VALUE(periodstarttime) OVER (RANGE BETWEEN CURRENT ROW AND INTERVAL '1' HOUR FOLLOWING ORDER BY periodstarttime)
||' to '||LAST_VALUE(periodstarttime) OVER (RANGE BETWEEN CURRENT ROW AND INTERVAL '1' HOUR FOLLOWING ORDER BY periodstarttime)
FROM ...
WHERE EXTRACT(MINUTE FROM periodstarttime) IN (0,15,30,45)

Oracle SQL Query to fetch non-working hours in a given month

Need your help to frame a Oracle SQL query to capture non-working hours in a given month for the list of trips.
Values passed will be start date and end date
E.g.:
Feb has totally 696 hrs
Start Date End Date
S2 - 02-Feb-16 14:00 - 06-Feb-16 20:00 - 102hrs
S1 - 01-Feb-16 04:00 - 02-Feb-16 10:00 - 30hrs
Total Worked hrs - 132 hrs
The query result expected is 564 hrs
Attempted Query:
SELECT (a)
FROM (
SELECT COUNT( (SELECT DISTINCT 'O'
FROM TRIP s,
TRUCK p
WHERE s.TRUCKID = p.TRUCKID
AND p.NOTES2='ABC'
AND p.TRUCKID='553'
AND ( to_date(d,'DD-Mon-YYYY HH24')
NOT BETWEEN to_date(s.STIME,'DD-Mon-YYYY HH24')
AND to_date(s.ETIME,'DD-Mon-YYYY HH24')
)
) ) a
FROM (
SELECT (TRUNC(to_date(sysdate,'DD-MON-YYYY'),'MM') + level - 1) d
FROM dual
CONNECT BY level <= TO_CHAR(LAST_DAY(to_date(sysdate,'DD-MON-YYYY')),'DD')
)
)
WITH Trip_Dates ( Start_Date, End_Date ) AS (
SELECT TO_DATE( '2016-02-02 14:00:00', 'YYYY-MM-DD HH24:MI:SS' ), TO_DATE( '2016-02-06 20:00:00', 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL UNION ALL
SELECT TO_DATE( '2016-02-01 04:00:00', 'YYYY-MM-DD HH24:MI:SS' ), TO_DATE( '2016-02-02 10:00:00', 'YYYY-MM-DD HH24:MI:SS' ) FROM DUAL
)
SELECT ( ADD_MONTHS( TRUNC( SYSDATE, 'MM' ), 1 ) - TRUNC( SYSDATE, 'MM' )
- SUM( End_Date - Start_Date ) ) * 24 AS Unworked_Hours
FROM Trip_Dates;
Outputs:
UNWORKED_HOURS
--------------
564