I am trying to write a query to get all nodes with their ancestors. The database stores a tree (nodes and their children/parents). I know that connect by can give all ancestors, and when coupled with the start with clause you can get all ancestors of a single node.
Here is a quick example to illustrate what I am going for.
Node edge table:
+---------+-----------+
|child_id |parent_id |
+---------+-----------+
|2 |1 |
|3 |2 |
|4 |2 |
|5 |4 |
+---------+-----------+
The query I wrote is:
select parent_id, child_id
from edges
start with child_id = 5
connect by child_id = prior parent_id
gives:
+---------+-----------+
|child_id |parent_id |
+---------+-----------+
|2 |1 |
|4 |2 |
|5 |4 |
+---------+-----------+
what I am looking for is something like this:
+---------+-----------+
|child_id |parent_id |
+---------+-----------+
|2 |1 |
|3 |2 |
|3 |1 |
|4 |2 |
|4 |1 |
|5 |4 |
|5 |2 |
|5 |1 |
+---------+-----------+
So each node has a record for each of it's ancestors all the way to the root. I'm having a bit of trouble building a query to get this result. Any suggestions?
Thanks, mcangus