I have a single table that can refer to one other member in the table as a parent. That parent could also refer to one other row as its parent...and so on.
id col1 col2 parentID
1 foo bar NULL
2 blah boo 1
3 fob far 2
4 wob lob NULL
I would like to return the chain given an id. So if the id were 3 I would return row 3, row 2 and row 1. If id was 2 I would return row 2 and row 1. If the id were 1 or 4 I would just return that row.
thank you
IF you use a recursive CTE, don't forget to add
on your join
else you will just a The maximum recursion-error since it loops
Use a recursive CTE:
Results:
Here you go