I need some help querying hierarchical data. Here is the single, simple, table, where parent_id
references id
and may be null for root entries.
create table edition (
id NUMBER(20),
parent_id NUMBER(20)
);
For each record in the table I need to find the deepest child having maximum id. If a record has no children, then its own id should be returned. I tried by myself but failed using START WITH A.id = B.id
where A and B are subqueries, looks like Oracle doesn't allow such joins.
Here is the sample data:
id parent_id
----------------------
1 NULL
2 1
3 1
4 1
5 4
6 5
7 5
and a sample result
id result
----------------------
1 7
2 2
3 3
4 7
5 7
6 6
7 7
I believe you want to try