Reputation: 17
My situation:
I have the following view to retrieve data from:
|ID | START_DATE | END_DATE |
|80 | 09-JAN-2013 15:01:52 | 20-SEP-2014 15:01:52 |
|82 | 09-SEP-2014 15:01:52 | 25-SEP-2014 15:01:52 |
What I want is something like this:
|TOTAL_TIME_IN_HOURS| MONTH| YEAR |
| 200 | 01 | 2013 |
| 250 | 02 | 2013 |
| etc..... | etc. | etc..|
| 150 | 09 | 2014 |
Some additional information: I can only use select statements, but I am able to create views beforehand. It's an Oracle DB, so I'm not able to use MYSQL functions like DATEDIFF etc.
I have done the following:
SELECT
ID,
SUM(END_TIME - START_TIME) * 24 AS TOTAL_TIME_IN_HOURS,
FROM TABLE_X
WHERE TO_CHAR(START_TIME, 'MM') IN (1,2,3,4,5,6,7,8,9,10,11,12) AND TO_CHAR(START_TIME, 'YYYY') BETWEEN 1965 AND 2050 AND
TO_CHAR(END_TIME, 'MM') IN (1,2,3,4,5,6,7,8,9,10,11,12) AND TO_CHAR(END_TIME, 'YYYY') BETWEEN 1965 AND 2050
GROUP BY PROC_ID, TO_CHAR(START_TIME, 'YYYY'), TO_CHAR(START_TIME, 'MM') ORDER BY ID;
This returns the following:
|ID| TOTAL_TIME_IN_HOURS |
|80| 5000 |
|82| 300 |
(I used fictitious results, because the question isn't about the factual results)
This logic is ok as far as I only need the total amount of hours between start and end date.. However what I need is the total amount of hours per month between start and end date.
I thought of adding additional columns to my views, like start_month, end_month, start_year and end_year. However I ran into new problems with these options, like leap years...
My question is: Is it possible to reach the result I want? If so what kind of logic should I use to reach this result? (Preferably a dynamic query, so I don't have to enter hundreds of lines of code)
Upvotes: 1
Views: 130
Reputation: 14209
A much quicker one with hierarchy query:
with w as
(
select distinct id,
greatest(start_date, trunc(add_months(start_date, level - 1), 'MON')) lim_low,
least(trunc(add_months(start_date, level), 'MON'), end_date) lim_high
from test t
connect by add_months(start_date, level - 1) <= end_date
order by 3, 1
)
select id, lim_low, (lim_high - lim_low) * 24 nb_hours
from w;
Upvotes: 1
Reputation: 3099
Another recursive solution, which will require at least Oracle 11gR2:
with t(id, start_date, end_date) as
(select 80, to_date('09/01/2013 15:01:52', 'DD/MM/YYYY HH24:MI:SS'), to_date('20/09/2014 15:01:52', 'DD/MM/YYYY HH24:MI:SS') from dual
union all
select 82, to_date('09/09/2014 15:01:52', 'DD/MM/YYYY HH24:MI:SS'), to_date('25/09/2014 15:01:52', 'DD/MM/YYYY HH24:MI:SS') from dual
)
, t_recur(id, start_date, end_date, month_start_date, month_end_date) as
(select id
, start_date
, end_date
, start_date
, least(add_months(trunc(start_date, 'MM'), 1), end_date)
from t
union all
select id
, start_date
, end_date
, trunc(add_months(month_start_date, 1), 'MM')
, least(add_months(trunc(month_start_date, 'MM'), 2), end_date)
from t_recur
where trunc(add_months(month_start_date, 1), 'MM') < end_date
)
select id
, extract(year from month_start_date) year
, extract(month from month_start_date) month
, (month_end_date - month_start_date) * 24 hours
from t_recur
order by id
, year
, month
Upvotes: 1
Reputation: 6476
Try this:
with t(ID, START_DATE, END_DATE) as (
select 80, to_date('09/01/2013 15:01:52', 'DD/MM/YYYY HH24:MI:SS'), to_date('20/09/2014 15:01:52', 'DD/MM/YYYY HH24:MI:SS') from dual union all
select 82, to_date('09/09/2014 15:01:52', 'DD/MM/YYYY HH24:MI:SS'), to_date('25/09/2014 15:01:52', 'DD/MM/YYYY HH24:MI:SS') from dual
), t_mon(id, start_date, end_date, lvl, year, month) as (
select id
, start_date
, least(trunc(add_months(start_date, 1), 'MONTH'), end_date)
, 1
, extract(year from start_date)
, extract(month from start_date)
from t
union all
select t.id
, greatest(trunc(add_months(t.start_date, lvl), 'MONTH'), t.start_date)
, least(trunc(add_months(t.start_date, lvl+1), 'MONTH'), t.end_date)
, lvl + 1
, extract(year from greatest(trunc(add_months(t.start_date, lvl), 'MONTH'), t.start_date))
, extract(month from greatest(trunc(add_months(t.start_date, lvl), 'MONTH'), t.start_date))
from t, t_mon
where trunc(add_months(t.start_date, t_mon.lvl), 'MONTH') < t.end_date
), t_corr(id, start_date, end_date, year, month) as (
select unique id, start_date, end_date, year, month
from t_mon
)
select id, year, month, sum(end_date - start_date) * 24 hours
from t_corr
group by id, year, month
order by id, year, month
ID YEAR MONTH HOURS
--------- ---------- ---------- ----------
80 2013 1 536,968889
80 2013 2 672
80 2013 3 744
80 2013 4 720
80 2013 5 744
80 2013 6 720
80 2013 7 744
80 2013 8 744
80 2013 9 720
80 2013 10 744
80 2013 11 720
80 2013 12 744
80 2014 1 744
80 2014 2 672
80 2014 3 744
80 2014 4 720
80 2014 5 744
80 2014 6 720
80 2014 7 744
80 2014 8 744
80 2014 9 471,031111
82 2014 9 384
Upvotes: 0