I've got hierarchical data in Oracle. Each Row has an id
, parent_id
and name
.
For each row I want to get
- the root ID (this is straightforward using
connect_by_root
)
- the ID of the node one level above root
- the ID of the node two levels above root
My baseline is
SELECT
id,
parent_id,
name,
connect_by_root id root,
<child_of_root_in_this_exact_path> child1_id,
<child_of_the_child_above> child2_id,
FROM table
START WITH id = 1
CONNECT BY PRIOR id = parent_id
The parent and grandparent of the child would be trivial (wouldn't need a hierachical query at all for this), but how do I find specific nodes from the opposite direction?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…