Sql Oracle-列出每个月的每一天和小时数
我做了一些仪表板,我发现我没有任何表格,包括我需要的所有日期。我的子查询通过日期DD.MM、YYYY和小时HH24进行连接。但是我被模板卡住了 我通过列出了一个月内的所有天数Sql Oracle-列出每个月的每一天和小时数,sql,oracle,datetime,recursive-query,Sql,Oracle,Datetime,Recursive Query,我做了一些仪表板,我发现我没有任何表格,包括我需要的所有日期。我的子查询通过日期DD.MM、YYYY和小时HH24进行连接。但是我被模板卡住了 我通过列出了一个月内的所有天数 select to_date('1.' || to_CHAR(sysdate, 'MM.YYYY'), 'dd.mm.yyyy') + level - 1 as month from dual connect by level <= TO_CHAR(LAST_DAY(sysdate), 'DD') 还列出
select to_date('1.' || to_CHAR(sysdate, 'MM.YYYY'), 'dd.mm.yyyy') + level - 1 as month
from dual
connect by level <= TO_CHAR(LAST_DAY(sysdate), 'DD')
还列出了通过
select to_date(to_char(sysdate, 'DD.MM.YYYY'), 'dd.mm.yyyy') + 1 / 24*(level - 1)
from dual
connect by level <= 24
当我把它和rownum=1结合在一起的时候,我只得到1天的工作时间
select to_date(to_char(mesic, 'DD.MM.YYYY'), 'dd.mm.yyyy') + 1 / 24*(level - 1)
from
(select *
from (select to_date('1.' || to_CHAR(sysdate, 'MM.YYYY'), 'dd.mm.yyyy') + level - 1 as mesic
from dual
connect by level <= TO_CHAR(LAST_DAY(sysdate), 'DD'))
where rownum = 1)
connect by level <= 24
但是,当我删除rownum条件时,我得到了最大行数
按照我的逻辑,这个月应该是24小时*31天=744。我的临时dashborads报告通常有5000行,因此这应该不是问题
感谢您的建议如果您希望生成当月的所有小时数,您可以执行以下操作:
with cte (dt) as (
select trunc(sysdate, 'month') from dual
union all
select dt + interval '1' hour
from cte
where dt + interval '1' hour < trunc(sysdate, 'month') + interval '1' month
)
select * from cte
我们可以通过分别生成天数和小时数,然后交叉连接来减少迭代次数:
with
days (dy) as (
select trunc(sysdate, 'month') from dual
union all
select dy + interval '1' day
from days
where dy + interval '1' day < trunc(sysdate, 'month') + interval '1' month
),
hours (hh) as (
select 0 from dual
union all
select hh + 1 from hours where hh < 23
)
select d.dy + h.hh * interval '1' hour dt
from days d
cross join hours h
如果要生成当月的所有小时数,可以执行以下操作:
with cte (dt) as (
select trunc(sysdate, 'month') from dual
union all
select dt + interval '1' hour
from cte
where dt + interval '1' hour < trunc(sysdate, 'month') + interval '1' month
)
select * from cte
我们可以通过分别生成天数和小时数,然后交叉连接来减少迭代次数:
with
days (dy) as (
select trunc(sysdate, 'month') from dual
union all
select dy + interval '1' day
from days
where dy + interval '1' day < trunc(sysdate, 'month') + interval '1' month
),
hours (hh) as (
select 0 from dual
union all
select hh + 1 from hours where hh < 23
)
select d.dy + h.hh * interval '1' hour dt
from days d
cross join hours h
对我来说,这看起来很简单
select trunc(sysdate, 'mm') + (level - 1) / 24 datum
from dual
connect by level <= (add_months(trunc(sysdate, 'mm'), 1) - trunc(sysdate, 'mm')) * 24;
DATUM
----------------
01.08.2020 00:00
01.08.2020 01:00
01.08.2020 02:00
<snip>
31.08.2020 21:00
31.08.2020 22:00
31.08.2020 23:00
744 rows selected.
对我来说,这看起来很简单
select trunc(sysdate, 'mm') + (level - 1) / 24 datum
from dual
connect by level <= (add_months(trunc(sysdate, 'mm'), 1) - trunc(sysdate, 'mm')) * 24;
DATUM
----------------
01.08.2020 00:00
01.08.2020 01:00
01.08.2020 02:00
<snip>
31.08.2020 21:00
31.08.2020 22:00
31.08.2020 23:00
744 rows selected.
@多米尼克瓦纳。如果这个或另一个答案回答了你的问题,那么你应该接受其中一个。@DominikVana。如果这个或另一个答案回答了你的问题,那么你应该接受其中一个。