分层SQL作为链接列表

时间:2019-05-21 07:43:06

标签: sql oracle12c hierarchical-data

我有一个数据集,该数据集表示创建一组产品的一组过程步骤。每个产品都有在数据集中定义的步骤序列,例如FromStep和ToStep。我正在尝试使用分层查询来拉动所有产品的流程步骤,但是我显然缺少了一些东西,因为它无法正常工作。任何关于我哪里出了问题的建议将不胜感激(也许这甚至不是解决此任务的方法)。我尝试在下面创建一个最小的问题示例-我的实际数据集比这个大得多。

创建示例表:

CREATE TABLE HIER_TEST    
(      
    PRODUCT     VARCHAR2(26 BYTE),    
    STEPNAME    VARCHAR2(26 BYTE),    
    STEPID      NUMBER(4,0),    
    FROMSTEP    NUMBER(4,0),    
    TOSTEP      NUMBER(4,0)   
) ;   

添加一些数据:

Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product1','Step1',1,1,2);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product1','Step2',2,2,3);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product1','Step3',3,3,4);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product1','Step4',4,4,5);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product1','Step5',5,5,6);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product2','Step1',1,1,2);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product2','Step2',2,2,3);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product2','Step3',3,3,4);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product2','Step4',4,4,5);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product2','Step5',5,5,6);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product5','Step1',1,1,2);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product5','Step2',2,2,3);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product5','Step3',3,3,4);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product5','Step4',4,4,5);   
Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP) values ('Product5','Step5',5,5,6);   

然后我要尝试以下查询:

select    
    level,   
    connect_by_isleaf leafnode,   
    hier_test.*,   
    connect_by_root stepid as rootItem   
from   
    hier_test       
start with   
    stepid = 1   
connect by nocycle prior   
    tostep = fromstep   
order siblings by   
    product,   
    stepid   
;   

返回:

+-------+-------+--------------+-----------+-------+-------+-------+-------+   
| Level | LfNd  |   Product    | StepName  | StepId| FrStp | ToStp | rtItm |   
+-------+-------+--------------+-----------+-------+-------+-------+-------+   
|   1   |   0   |   Product1   |   Step1   |   1   |   1   |   2   |   1   |   
|   2   |   0   |   Product1   |   Step2   |   2   |   2   |   3   |   1   |   
|   3   |   0   |   Product1   |   Step3   |   3   |   3   |   4   |   1   |   
|   4   |   0   |   Product1   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product1   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product2   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product5   |   Step5   |   5   |   5   |   6   |   1   |   
|   4   |   0   |   Product2   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product1   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product2   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product5   |   Step5   |   5   |   5   |   6   |   1   |   
|   4   |   0   |   Product5   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product1   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product2   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product5   |   Step5   |   5   |   5   |   6   |   1   |   
|   3   |   0   |   Product2   |   Step3   |   3   |   3   |   4   |   1   |   
|   4   |   0   |   Product1   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product1   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product2   |   Step5   |   5   |   5   |   6   |   1   |   
|   5   |   1   |   Product5   |   Step5   |   5   |   5   |   6   |   1   |   

This appears to be locked in a loop forever...

我希望order siblings by子句按产品然后按步骤号对表进行排序,但是结果没有显示出来。

我的目标是:

+-------+-------+--------------+-----------+-------+-------+-------+-------+   
| Level | LfNd  |   Product    | StepName  | StepId| FrStp | ToStp | rtItm |   
+-------+-------+--------------+-----------+-------+-------+-------+-------+   
|   1   |   0   |   Product1   |   Step1   |   1   |   1   |   2   |   1   |   
|   2   |   0   |   Product1   |   Step2   |   2   |   2   |   3   |   1   |   
|   3   |   0   |   Product1   |   Step3   |   3   |   3   |   4   |   1   |   
|   4   |   0   |   Product1   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product1   |   Step5   |   5   |   5   |   6   |   1   |   
|   1   |   0   |   Product2   |   Step1   |   1   |   1   |   2   |   1   |   
|   2   |   0   |   Product2   |   Step2   |   2   |   2   |   3   |   1   |   
|   3   |   0   |   Product2   |   Step3   |   3   |   3   |   4   |   1   |   
|   4   |   0   |   Product2   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product2   |   Step5   |   5   |   5   |   6   |   1   |   
|   1   |   0   |   Product5   |   Step1   |   1   |   1   |   2   |   1   |   
|   2   |   0   |   Product5   |   Step2   |   2   |   2   |   3   |   1   |   
|   3   |   0   |   Product5   |   Step3   |   3   |   3   |   4   |   1   |   
|   4   |   0   |   Product5   |   Step4   |   4   |   4   |   5   |   1   |   
|   5   |   1   |   Product5   |   Step5   |   5   |   5   |   6   |   1   |   

我敢肯定,这很明显,但是在尝试找到解决方案时,我发现什么都没有真正帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

结果显示出来。

CREATE TABLE HIER_TEST    
  (      
  PRODUCT     VARCHAR2(26 BYTE),    
  STEPNAME    VARCHAR2(26 BYTE),    
  STEPID      NUMBER(4,0),    
  FROMSTEP    NUMBER(4,0),    
  TOSTEP      NUMBER(4,0)   
);

采用较小的数据子集,以便于可视化:

Insert into HIER_TEST (PRODUCT,STEPNAME,STEPID,FROMSTEP,TOSTEP)
SELECT 'Product1','Step1',1,1,2 FROM DUAL UNION ALL   
SELECT 'Product1','Step2',2,2,3 FROM DUAL UNION ALL   
SELECT 'Product1','Step3',3,3,4 FROM DUAL UNION ALL
SELECT 'Product2','Step1',1,1,2 FROM DUAL UNION ALL   
SELECT 'Product2','Step2',2,2,3 FROM DUAL UNION ALL   
SELECT 'Product2','Step3',3,3,4 FROM DUAL UNION ALL
SELECT 'Product5','Step1',1,1,2 FROM DUAL UNION ALL   
SELECT 'Product5','Step2',2,2,3 FROM DUAL UNION ALL   
SELECT 'Product5','Step3',3,3,4 FROM DUAL;

然后在查询中添加SYS_CONNECT_BY_PATH( Product, ', ' )(由于数据中没有任何循环,因此不需要NOCYCLE子句)

select level,   
       connect_by_isleaf leaf,
       product,
       stepname AS sname,
       stepid AS id,
       fromstep AS fs,
       tostep AS ts,
       connect_by_root stepid as rt,
       SYS_CONNECT_BY_PATH( product, ', ' ) As path
from   hier_test       
start with
       stepid = 1   
connect by
       prior tostep = fromstep   
order siblings by   
        product,   
        stepid

给出输出:

LEVEL | LEAF | PRODUCT  | SNAME | ID | FS | TS | RT | PATH                          
----: | ---: | :------- | :---- | -: | -: | -: | -: | :-----------------------------
    1 |    0 | Product1 | Step1 |  1 |  1 |  2 |  1 | , Product1                    
    2 |    0 | Product1 | Step2 |  2 |  2 |  3 |  1 | , Product1, Product1          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product1, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product1, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product1, Product5
    2 |    0 | Product2 | Step2 |  2 |  2 |  3 |  1 | , Product1, Product2          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product2, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product2, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product2, Product5
    2 |    0 | Product5 | Step2 |  2 |  2 |  3 |  1 | , Product1, Product5          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product5, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product5, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product5, Product5
    1 |    0 | Product2 | Step1 |  1 |  1 |  2 |  1 | , Product2                    
    2 |    0 | Product1 | Step2 |  2 |  2 |  3 |  1 | , Product2, Product1          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product1, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product1, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product1, Product5
    2 |    0 | Product2 | Step2 |  2 |  2 |  3 |  1 | , Product2, Product2          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product2, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product2, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product2, Product5
    2 |    0 | Product5 | Step2 |  2 |  2 |  3 |  1 | , Product2, Product5          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product5, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product5, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product5, Product5
    1 |    0 | Product5 | Step1 |  1 |  1 |  2 |  1 | , Product5                    
    2 |    0 | Product1 | Step2 |  2 |  2 |  3 |  1 | , Product5, Product1          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product1, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product1, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product1, Product5
    2 |    0 | Product2 | Step2 |  2 |  2 |  3 |  1 | , Product5, Product2          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product2, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product2, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product2, Product5
    2 |    0 | Product5 | Step2 |  2 |  2 |  3 |  1 | , Product5, Product5          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product5, Product1
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product5, Product2
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product5, Product5

然后查看路径,您可以看到它由同级在第一层,第二层和第三层的顺序排列。因为您只是在当前深度查看产品(等),而没有看到此订购。

如果要将其限制为具有与上一级相同的产品,则将其添加到CONNECT BY子句中:

select level,   
       connect_by_isleaf leaf,
       product,
       stepname AS sname,
       stepid AS id,
       fromstep AS fs,
       tostep AS ts,
       connect_by_root stepid as rt,
       SYS_CONNECT_BY_PATH( product, ', ' ) As path
from   hier_test       
start with
       stepid = 1   
connect by
       prior tostep  = fromstep
and    prior product = product
order siblings by   
        product,   
        stepid

哪个输出:

LEVEL | LEAF | PRODUCT  | SNAME | ID | FS | TS | RT | PATH                          
----: | ---: | :------- | :---- | -: | -: | -: | -: | :-----------------------------
    1 |    0 | Product1 | Step1 |  1 |  1 |  2 |  1 | , Product1                    
    2 |    0 | Product1 | Step2 |  2 |  2 |  3 |  1 | , Product1, Product1          
    3 |    1 | Product1 | Step3 |  3 |  3 |  4 |  1 | , Product1, Product1, Product1
    1 |    0 | Product2 | Step1 |  1 |  1 |  2 |  1 | , Product2                    
    2 |    0 | Product2 | Step2 |  2 |  2 |  3 |  1 | , Product2, Product2          
    3 |    1 | Product2 | Step3 |  3 |  3 |  4 |  1 | , Product2, Product2, Product2
    1 |    0 | Product5 | Step1 |  1 |  1 |  2 |  1 | , Product5                    
    2 |    0 | Product5 | Step2 |  2 |  2 |  3 |  1 | , Product5, Product5          
    3 |    1 | Product5 | Step3 |  3 |  3 |  4 |  1 | , Product5, Product5, Product5

db <>提琴here

相关问题