I have a parents table looks like this
CHILD_ID | PARENT_ID | NAME
1 | Null | Bill
2 | 1 | Jane
3 | 1 | Steve
4 | 2 | Ben
5 | 3 | Andrew
Id like to get a result set like this
Bill
---Jane
------Ben
---Steve
------Andrew
I know I need to do a rank query to rank the levels and a self join but all I can find on the net is CTE recursion
I have done this in Oracle before but not in MS SQL
Bit hacky and can be improved but hopefully it shows the principle...
If however you want to avoid recursion then an alternative approach is to implement a tree table with the relevant tree structure information - see http://www.sqlteam.com/article/more-trees-hierarchies-in-sql for a walk through
This was a tough one:). I expanded the example to include > 1 tree. Results looking good so far.