在选择语句中添加增量索引

时间:2018-10-10 20:19:30

标签: sql oracle

我有一个简单的SELECT语句:

SELECT WORKORDERID AS WOID FROM WORKORDER WO

这将返回我的WO表中的工作单ID列表。

   WOID
  |AAB1|
  |AAB2|
  |AAB3|

但是我想为每个工单ID添加索引号,如下所示:

SELECT WORKORDERID ||'-'||[index] AS WOID FROM WORKORDER WO

我想要的结果是:

       WOID
      |AAB1-1|
      |AAB1-2|
      |AAB1-3|
      |AAB1-4|
      |AAB2-1|
      |AAB2-2|
      |AAB2-3|
      |AAB2-4|
      |AAB3-1|
      |AAB3-2|
      |AAB3-3|
      |AAB3-4|

我能想到的唯一方法是用索引1-4创建另一个表,然后将这两个表连接起来。我的问题是,是否可以在一个SELECT语句中实现此结果而不必创建另一个表?

3 个答案:

答案 0 :(得分:1)

您在这里:

Application.yaml

 kafka:
   security:
     keytab: classpath:test.keytab

Logback.xml  

<springProperty scope="context" name="keytab" 
  source="kafka.security.keytab"/>

答案 1 :(得分:0)

您可以使用cross join

select wo.woid || '-' || n.n as new_woid
from WORKORDER wo cross join
     (select 1 as n from dual union all
      select 2 as n from dual union all
      select 3 as n from dual union all
      select 4 as n from dual
     ) n

答案 2 :(得分:0)

可能类似于:

with idx (n) as (
  select 1 from dual union all
  select 2 from dual union all
  select 3 from dual union all
  select 4 from dual
)
select
    wo.workorderid || '-' || idx.n
  from workorder wo, idx