По сути, вы определяете иерархию сверху вниз или сверху вниз.
Посмотрите на эти примеры, чтобы увидеть разницу:
WITH t(person, parent_id, ID) AS (
SELECT 'Grandma', NULL, 1 FROM dual
UNION ALL SELECT 'Mother', 1, 10 FROM dual
UNION ALL SELECT 'Daughter', 10, 100 FROM dual
UNION ALL SELECT 'Son', 10, 101 FROM dual)
SELECT person AS leaf_person, CONNECT_BY_ROOT(person) AS top_person,
SYS_CONNECT_BY_PATH(person, '->'), 'down' AS direction
FROM t
WHERE CONNECT_BY_ISLEAF = 1
START WITH parent_id IS NULL
CONNECT BY parent_id = PRIOR ID
UNION ALL
SELECT person as leaf_person, CONNECT_BY_ROOT(person) as top_person,
SYS_CONNECT_BY_PATH(person, '->'), 'up' AS direction
FROM t
WHERE CONNECT_BY_ISLEAF = 1
START WITH ID IN (100,101)
CONNECT BY PRIOR parent_id = ID;
+-----------------------------------------------------------------+
|LEAF_PERSON|TOP_PERSON|SYS_CONNECT_BY_PATH(PERSON,'->')|DIRECTION|
+-----------------------------------------------------------------+
|Daughter |Grandma |->Grandma->Mother->Daughter |down |
|Son |Grandma |->Grandma->Mother->Son |down |
|Grandma |Daughter |->Daughter->Mother->Grandma |up |
|Grandma |Son |->Son->Mother->Grandma |up |
+-----------------------------------------------------------------+
Обычно у вас есть только один root (то есть parent_id IS NULL
) или, по крайней мере, определенный root элемент, таким образом В большинстве примеров используется направление «сверху вниз».