这样的查询有什么好处吗?
the_whole_month CTE 创建每个月的所有日期;表函数调用是为了避免重复
SQL> with test (first_day, value) as
2 (select date '2018-01-01', 100 from dual union all
3 select date '2018-02-01', 200 from dual union all
4 select date '2018-03-01', 300 from dual
5 ),
6 the_whole_month as
7 (select (first_day + column_value - 1) datum, value
8 from test,
9 table(cast(multiset(select level from dual
10 connect by level <= last_day(first_day) - first_day + 1
11 ) as sys.odcinumberlist))
12 )
13 select * from the_whole_month
14 order by datum;
DATUM VALUE
-------- ----------
01.01.18 100 -- January begins here
02.01.18 100
<snip>
29.01.18 100
30.01.18 100
31.01.18 100
01.02.18 200 -- February begins here
02.02.18 200
<snip>
27.02.18 200
28.02.18 200
01.03.18 300 -- March begins here
02.03.18 300
<snip>
29.03.18 300
30.03.18 300
31.03.18 300
90 rows selected.
SQL>
关于重复项(你会得到没有表功能):查询看起来像这样;我把这几个月缩短到每个只有 3 天。您预计 3 monts x 3 天 = 9 行,但您将获得 39 行。如果您有足够的耐心运行它整个月,并且在您拥有的所有月份中,您可能会得到......我不知道,数百万行。不要那样做。
SQL> with test (first_day, value) as
2 (select date '2018-01-01', 100 from dual union all
3 select date '2018-02-01', 200 from dual union all
4 select date '2018-03-01', 300 from dual
5 ),
6 the_whole_month as
7 (select (first_day + level - 1) datum, value
8 from test
9 connect by level <= 3 --> Presuming there are only 3 days in every month.
10 --> You'd expect 3 x 3 = 9 rows, but you'll get 39.
11 )
12 select * from the_whole_month
13 order by datum;
DATUM VALUE
-------- ----------
01.01.18 100
02.01.18 100
02.01.18 100
02.01.18 100
03.01.18 100
03.01.18 100
03.01.18 100
<snip>
03.03.18 300
03.03.18 300
39 rows selected.
SQL>