我的情况:
我有以下视图从中检索数据:
|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 |
我想要的是这样的:
|TOTAL_TIME_IN_HOURS| MONTH| YEAR |
| 200 | 01 | 2013 |
| 250 | 02 | 2013 |
| etc..... | etc. | etc..|
| 150 | 09 | 2014 |
其他一些信息: 我只能使用select语句,但我可以事先创建视图。 它是一个Oracle数据库,因此我无法使用DATEDIFF等MYSQL函数。
我做了以下事情:
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;
返回以下内容:
|ID| TOTAL_TIME_IN_HOURS |
|80| 5000 |
|82| 300 |
(我使用虚构结果,因为问题不是关于事实结果)
这个逻辑是可以的,因为我只需要开始日期和结束日期之间的总小时数。但我需要的是开始日期和结束日期之间每月的总小时数。
我想在我的视图中添加其他列,例如start_month,end_month,start_year和end_year。但是我遇到了这些选项的新问题,比如闰年......
我的问题是:是否有可能达到我想要的结果?如果是这样,我应该使用什么样的逻辑来达到这个结果? (最好是动态查询,因此我不必输入数百行代码)
答案 0 :(得分:1)
另一个递归解决方案,至少需要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
答案 1 :(得分:1)
更快的层次结构查询:
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;
答案 2 :(得分:0)
试试这个:
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