Результаты показывают это.
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 <> Fiddle здесь