这基本上是一个生成行的连接查询,例如生成从 1 到 10 的行,使用:
select level as x
from dual connect by level <= 10;
| X |
|----|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
为了生成从 5 到 10 的行,请使用:
select 5 + level - 1 as x
from dual connect by level <= 10 - 5 + 1;
| X |
|----|
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
为了生成2017-01-02
使用日期2017-01-05
:
select date '2017-01-02' + level - 1 as x
from dual connect by level <= date '2017-01-05' - date '2017-01-02' + 1;
| X |
|----------------------|
| 2017-01-02T00:00:00Z |
| 2017-01-03T00:00:00Z |
| 2017-01-04T00:00:00Z |
| 2017-01-05T00:00:00Z |
如果您使用的是 Oracle 12c,那么您可以使用 LATERAL 或 CROSSAPPLY 来运行后一个生成器查询,形成许多来自源子查询或表的 start+end 值,请考虑以下示例:
create table probe(
source_rel_id int,
source_rel_start date,
source_rel_end date
);
insert into probe values( 1, date '2017-01-02', date '2017-01-05' );
insert into probe values( 2, date '2017-03-01', date '2017-03-15' );
insert into probe values( 3, date '2017-05-05', date '2017-05-30' );
commit;
select * from probe p
cross apply (
select p.source_rel_start + level - 1 as my_date
from dual connect by level <= p.source_rel_end - p.source_rel_start + 1
)
SOURCE_REL_ID SOURCE_REL_START SOURCE_REL_END MY_DATE
------------- ---------------- ---------------- ----------------
1 2017/01/02 00:00 2017/01/05 00:00 2017/01/02 00:00
1 2017/01/02 00:00 2017/01/05 00:00 2017/01/03 00:00
1 2017/01/02 00:00 2017/01/05 00:00 2017/01/04 00:00
1 2017/01/02 00:00 2017/01/05 00:00 2017/01/05 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/01 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/02 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/03 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/04 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/05 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/06 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/07 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/08 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/09 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/10 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/11 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/12 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/13 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/14 00:00
2 2017/03/01 00:00 2017/03/15 00:00 2017/03/15 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/05 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/06 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/07 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/08 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/09 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/10 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/11 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/12 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/13 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/14 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/15 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/16 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/17 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/18 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/19 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/20 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/21 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/22 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/23 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/24 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/25 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/26 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/27 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/28 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/29 00:00
3 2017/05/05 00:00 2017/05/30 00:00 2017/05/30 00:00
编辑
如果我没有 12c,我还有什么其他选择?我应该检查版本,但我记得是 11。
只需生成从 1 到 N 的一系列数字,其中 N 必须大于您要为一条记录生成的最大数字数,以这种方式:
SELECT level as xyz FROM dual
CONNECT BY LEVEL <= 10000
然后以这种方式将上述查询的结果加入到表中 --> 请看这个演示
SELECT source_rel_id, source_rel_start, source_rel_end,
source_rel_start + xyz - 1 As days
FROM (
SELECT level as xyz FROM dual
CONNECT BY LEVEL <= 10000
) x
JOIN probe p
ON xyz <= source_rel_end - source_rel_start + 1
ORDER BY source_rel_id, days
| SOURCE_REL_ID | SOURCE_REL_START | SOURCE_REL_END | DAYS |
|---------------|----------------------|----------------------|----------------------|
| 1 | 2017-01-02T00:00:00Z | 2017-01-05T00:00:00Z | 2017-01-02T00:00:00Z |
| 1 | 2017-01-02T00:00:00Z | 2017-01-05T00:00:00Z | 2017-01-03T00:00:00Z |
| 1 | 2017-01-02T00:00:00Z | 2017-01-05T00:00:00Z | 2017-01-04T00:00:00Z |
| 1 | 2017-01-02T00:00:00Z | 2017-01-05T00:00:00Z | 2017-01-05T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-01T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-02T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-03T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-04T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-05T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-06T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-07T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-08T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-09T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-10T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-11T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-12T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-13T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-14T00:00:00Z |
| 2 | 2017-03-01T00:00:00Z | 2017-03-15T00:00:00Z | 2017-03-15T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-05T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-06T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-07T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-08T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-09T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-10T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-11T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-12T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-13T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-14T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-15T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-16T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-17T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-18T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-19T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-20T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-21T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-22T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-23T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-24T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-25T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-26T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-27T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-28T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-29T00:00:00Z |
| 3 | 2017-05-05T00:00:00Z | 2017-05-30T00:00:00Z | 2017-05-30T00:00:00Z |
我用过 10000,应该足够了,因为 10000 天超过 27 年,所以我认为您不需要为每条记录生成 27 年的日期,但如果这仍然太少,那么实际上可以一个数字在 100 万到 1000 万之间。由于行是在内存中生成的,因此在某些时候(1~10 百万)你会遇到内存不足的错误。
您还可以使用子查询来准确计算此限制:
SELECT level as xyz FROM dual
CONNECT BY LEVEL <= (
SELECT max( source_rel_end - source_rel_start ) + 1
FROM probe
)