Search code examples
oracle-databaseoracle12c

Hierarchical query get all children as rows


Data:

ID  PARENT_ID
1   [null]
2   1
3   1
4   2

Desired result:

ID  CHILD_AT_ANY_LEVEL
1   2
1   3
1   4
2   4

I've tried SYS_CONNECT_BY_PATH, but I don't understand how to convert it result into "inline view" which I can use for JOIN with main table.


Solution

  • select connect_by_root(id) id, id child_at_any_level
    from table
    where level <> 1
    connect by prior id = parent_id;