This is for SQL Server 2008 R2, I'm a novice at SQL so please be as specific as you can.
Table1
has some recursive structure built into it, where the ParentId
is either Null
meaning it's the root, or ParentId
is the Id
of another row in Table1
which denotes it as a child.
Example data set:
Table1Id ParentId
--------------------------------------------
1 NULL
2 1
3 1
4 2
5 NULL
6 2
7 6
8 NULL
9 8
With the above example the table then has the following tree structure with 3 root nodes:
Root 1 5 8
Child(teir1) 2 3 9
Child(teir2) 4 6
Child(tier3) 7
....
Is there a way to return only the Root row given any of the row Ids? For example:
InputId ReturnedRowId
----------------------------
1 1
2 1
3 1
4 1
5 5
6 1
7 1
8 8
9 8
Any help would be appreciated.