If there is a table which as 2 columns, ParentId and ChildId and there could be multiple levels, as table below
ParentId ChildId
-1 1
-1 2
1 3
3 4
4 5
If the child has no parent it is indicated by -1.
Now if I need the parent at the topmost level for say 5 , then its parent is 4 whos parent is 3 whos parent is 1. So 1 is the answer as its parent = -1.
I went through the discussion in Sql server CTE and recursion example, seemed complex.
From googling I got to know that we can use WITH AS and UNION to achieve this, can any one shed some light on a generic sql that can get this result ?
thanks
Nohsib