oracle 如何在PL SQL中迭代地向Select结果集添加行?

bksxznpy  于 2023-01-20  发布在  Oracle
关注(0)|答案(1)|浏览(207)

在work_order表中有wo_no。当我查询work_order表时,我希望在结果集中有2个额外的列(Task_no、Task_step_no),如下所示

这应该在work_order表中为所有的wo_no重复。task_no应该上升到5并且task_step_no应该上升到2000。(如果不清楚,请看所附的图像以查看结果集)
知道如何在plsql中获得这样的结果集吗?

6kkfgxo0

6kkfgxo01#

一个选项是使用交叉联接到当前表的2个行生成器。

SQL> with
  2  work_order (wo_no) as
  3    (select 1 from dual union all
  4     select 2 from dual
  5    ),
  6  task (task_no) as
  7    (select level from dual connect by level <= 5),
  8  step (task_step_no) as
  9    (select level from dual connect by level <= 20)    --> you'd have 2000 here
 10  select y.wo_no, t.task_no, s.task_step_no
 11  from work_order y cross join task t cross join step s
 12  order by 1, 2, 3;

结果:

WO_NO    TASK_NO TASK_STEP_NO
---------- ---------- ------------
         1          1            1
         1          1            2
         1          1            3
         1          1            4
         1          1            5
         1          1            6
         1          1            7
         1          1            8
         1          1            9
         1          1           10
         1          1           11
         1          1           12
         1          1           13
         1          1           14
         1          1           15
         1          1           16
         1          1           17
         1          1           18
         1          1           19
         1          1           20
         1          2            1
         1          2            2
         1          2            3
         1          2            4
         1          2            5
         1          2            6
         1          2            7
         1          2            8
         1          2            9
         1          2           10
         1          2           11
         1          2           12
         1          2           13
         1          2           14
         1          2           15
         1          2           16
         1          2           17
         1          2           18
         1          2           19
         1          2           20
         1          3            1
         1          3            2
         1          3            3
         1          3            4
         1          3            5
         1          3            6
         1          3            7
         1          3            8
         1          3            9
         1          3           10
         1          3           11
         1          3           12
         1          3           13
         1          3           14
         1          3           15
         1          3           16
         1          3           17
         1          3           18
         1          3           19
         1          3           20
         1          4            1
         1          4            2
         1          4            3
         1          4            4
         1          4            5
         1          4            6
         1          4            7
         1          4            8
         1          4            9
         1          4           10
         1          4           11
         1          4           12
         1          4           13
         1          4           14
         1          4           15
         1          4           16
         1          4           17
         1          4           18
         1          4           19
         1          4           20
         1          5            1
         1          5            2
         1          5            3
         1          5            4
         1          5            5
         1          5            6
         1          5            7
         1          5            8
         1          5            9
         1          5           10
         1          5           11
         1          5           12
         1          5           13
         1          5           14
         1          5           15
         1          5           16
         1          5           17
         1          5           18
         1          5           19
         1          5           20
         2          1            1
         2          1            2
         2          1            3
         2          1            4
         2          1            5
         2          1            6
         2          1            7
         2          1            8
         2          1            9
         2          1           10
         2          1           11
         2          1           12
         2          1           13
         2          1           14
         2          1           15
         2          1           16
         2          1           17
         2          1           18
         2          1           19
         2          1           20
         2          2            1
         2          2            2
         2          2            3
         2          2            4
         2          2            5
         2          2            6
         2          2            7
         2          2            8
         2          2            9
         2          2           10
         2          2           11
         2          2           12
         2          2           13
         2          2           14
         2          2           15
         2          2           16
         2          2           17
         2          2           18
         2          2           19
         2          2           20
         2          3            1
         2          3            2
         2          3            3
         2          3            4
         2          3            5
         2          3            6
         2          3            7
         2          3            8
         2          3            9
         2          3           10
         2          3           11
         2          3           12
         2          3           13
         2          3           14
         2          3           15
         2          3           16
         2          3           17
         2          3           18
         2          3           19
         2          3           20
         2          4            1
         2          4            2
         2          4            3
         2          4            4
         2          4            5
         2          4            6
         2          4            7
         2          4            8
         2          4            9
         2          4           10
         2          4           11
         2          4           12
         2          4           13
         2          4           14
         2          4           15
         2          4           16
         2          4           17
         2          4           18
         2          4           19
         2          4           20
         2          5            1
         2          5            2
         2          5            3
         2          5            4
         2          5            5
         2          5            6
         2          5            7
         2          5            8
         2          5            9
         2          5           10
         2          5           11
         2          5           12
         2          5           13
         2          5           14
         2          5           15
         2          5           16
         2          5           17
         2          5           18
         2          5           19
         2          5           20

200 rows selected.

SQL>

由于您已经有了work_order表,您只需在FROM子句中使用它(而不是作为CTE):

with
task (task_no) as
  (select level from dual connect by level <= 5),
step (task_step_no) as
  (select level from dual connect by level <= 20)
select y.wo_no, t.task_no, s.task_step_no
from work_order y cross join task t cross join step s
order by 1, 2, 3;

相关问题