如何生成 2 个值之间的值?
How to generate values between 2 values?
我的数据如下所示
with test(col) as (
select '01-06' from dual union all
select '45-52' from dual
) select col from test ;
需要OP
COL
01
02
.
.
.
06
45
46
.
.
52
其实我的table数是2万。我用connect by 但是速度很慢。
您可以使用递归查询生成值:
with test(col) as (
select '01-06' from dual union all
select '45-52' from dual
), bounds (l,u) as (
select to_number(substr(col,1,2)), to_number(substr(col,4,2)) from test
), r (l,u) as (
select l,u from bounds
union all
select r.l + 1, r.u from r where r.l < r.u
)
select to_char(l,'00') from r order by l;
(如果任何值不是 2 位数字,则适当编辑 substr 表达式)
说“我使用 connect by 但它很慢”而不发布您编写的代码并没有多大帮助。我 假设 你做错了,即有太多重复值导致速度变慢。看看这个 connect by
选项是否有帮助。
SQL> with test (col)
2 as
3 (select '1-6' from dual
4 union all
5 select '45-52' from dual
6 )
7 select lpad(to_number(substr(col, 1, instr(col, '-') - 1)) + column_value - 1, 2, '0') val
8 from test cross join
9 table(cast(multiset(select level from dual
10 connect by level <= to_number(substr(col, instr(col, '-') + 1)) -
11 to_number(substr(col, 1, instr(col, '-') - 1)) + 1
12 ) as sys.odcinumberlist));
VAL
---
01
02
03
04
05
06
45
46
47
48
49
50
51
52
14 rows selected.
SQL>
我的数据如下所示
with test(col) as (
select '01-06' from dual union all
select '45-52' from dual
) select col from test ;
需要OP
COL
01
02
.
.
.
06
45
46
.
.
52
其实我的table数是2万。我用connect by 但是速度很慢。
您可以使用递归查询生成值:
with test(col) as (
select '01-06' from dual union all
select '45-52' from dual
), bounds (l,u) as (
select to_number(substr(col,1,2)), to_number(substr(col,4,2)) from test
), r (l,u) as (
select l,u from bounds
union all
select r.l + 1, r.u from r where r.l < r.u
)
select to_char(l,'00') from r order by l;
(如果任何值不是 2 位数字,则适当编辑 substr 表达式)
说“我使用 connect by 但它很慢”而不发布您编写的代码并没有多大帮助。我 假设 你做错了,即有太多重复值导致速度变慢。看看这个 connect by
选项是否有帮助。
SQL> with test (col)
2 as
3 (select '1-6' from dual
4 union all
5 select '45-52' from dual
6 )
7 select lpad(to_number(substr(col, 1, instr(col, '-') - 1)) + column_value - 1, 2, '0') val
8 from test cross join
9 table(cast(multiset(select level from dual
10 connect by level <= to_number(substr(col, instr(col, '-') + 1)) -
11 to_number(substr(col, 1, instr(col, '-') - 1)) + 1
12 ) as sys.odcinumberlist));
VAL
---
01
02
03
04
05
06
45
46
47
48
49
50
51
52
14 rows selected.
SQL>