Suppose I have a table of objects structured in a hierarchy:
A
|--B
|--C
| +--D
+--E
They are stored in a "parent-child" table thus:
parent child
A B
A C
C D
A E
How do I query this to get the structure defined above? I think I need something that produces info like this:
object full_path
A NULL
B A
C A
D A.C
E A
I cannot figure out how to do the objects nested more than one level deep. It feels like I might need to iterate over the table (no idea if this is possible in SQL), or otherwise use some kind of query I've never encountered before.
Additional Info:
- A need not be the only orphan object.
- Children may have multiple parents BUT for now I'm happy with an answer to whichever scenario is easier to solve. I can abide converting a multi-parent structure to a single parent with a simple
GROUP BY
andMIN
statement.
left join
query. – cammil