分层 SQL 作为链表
Hierarchical SQL as linked list
我有一个数据集,代表创建一组产品的一组流程步骤。每个产品都有一个在数据集中定义的步骤序列,如 FromStep 和 ToStep。我正在尝试使用分层查询来提取所有产品的流程步骤,但我显然遗漏了一些东西,因为它不起作用。任何关于我哪里出错的建议都将不胜感激(也许这甚至不是完成此任务的方法)。我试图在下面创建一个我的问题的最小示例 - 我的真实数据集比这大得多。
创建样本table:
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
;
哪个returns:
+-------+-------+--------------+-----------+-------+-------+-------+-------+
| 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 子句会先按产品然后按步骤编号对 table 进行排序,但结果并未显示这一点。
我的目标是:
+-------+-------+--------------+-----------+-------+-------+-------+-------+
| 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 |
我相信这很明显,但我在尝试寻找解决方案时没有发现任何真正帮助我的东西。
提前致谢。
结果正在显示。
CREATE TABLE HIER_TEST
(
PRODUCT VARCHAR2(26 BYTE),
STEPNAME VARCHAR2(26 BYTE),
STEPID NUMBER(4,0),
FROMSTEP NUMBER(4,0),
TOSTEP NUMBER(4,0)
);
使用较小的 sub-set 数据以便更容易可视化:
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<>fiddle here
我有一个数据集,代表创建一组产品的一组流程步骤。每个产品都有一个在数据集中定义的步骤序列,如 FromStep 和 ToStep。我正在尝试使用分层查询来提取所有产品的流程步骤,但我显然遗漏了一些东西,因为它不起作用。任何关于我哪里出错的建议都将不胜感激(也许这甚至不是完成此任务的方法)。我试图在下面创建一个我的问题的最小示例 - 我的真实数据集比这大得多。
创建样本table:
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
;
哪个returns:
+-------+-------+--------------+-----------+-------+-------+-------+-------+
| 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 子句会先按产品然后按步骤编号对 table 进行排序,但结果并未显示这一点。
我的目标是:
+-------+-------+--------------+-----------+-------+-------+-------+-------+
| 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 |
我相信这很明显,但我在尝试寻找解决方案时没有发现任何真正帮助我的东西。
提前致谢。
结果正在显示。
CREATE TABLE HIER_TEST
(
PRODUCT VARCHAR2(26 BYTE),
STEPNAME VARCHAR2(26 BYTE),
STEPID NUMBER(4,0),
FROMSTEP NUMBER(4,0),
TOSTEP NUMBER(4,0)
);
使用较小的 sub-set 数据以便更容易可视化:
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<>fiddle here