【问题标题】:How to generate values between 2 values?如何在 2 个值之间生成值?
【发布时间】:2021-11-16 13:11:26
【问题描述】:

我的数据如下所示

with test(col) as (
    select  '01-06' from dual union all
    select  '45-52' from dual
    ) select col from test ;

必需的操作

COL
01
02
.
.
.
06
45
46
.
.
52

实际上我的桌数是 20,000。我用过connect by,但速度很慢。

【问题讨论】:

    标签: sql oracle oracle11g


    【解决方案1】:

    您可以使用递归查询生成值:

    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 表达式)

    【讨论】:

      【解决方案2】:

      在不发布您编写的代码的情况下说“我使用了 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>
      

      【讨论】:

        猜你喜欢
        • 2021-11-10
        • 1970-01-01
        • 1970-01-01
        • 2018-01-25
        • 1970-01-01
        • 1970-01-01
        • 2012-11-16
        • 1970-01-01
        • 2015-08-15
        相关资源
        最近更新 更多