I have a set of hierarchical data that I'm hoping to get into the right format without using a loop. Maybe it's a long day and I just don't see how to do it.
When it starts the data looks like this: (row = row ID, par = parent row ID, lev = level, has_child is obvious, had to do that or edit the space between all those numbers!)
row par lev has_child
1 NUL 0 1
2 1 1 1
3 1 1 1
4 1 1 1
5 1 1 1
6 1 1 0
148 2 2 0
149 2 2 1
145 3 2 0
146 3 2 1
9 4 2 0
11 4 2 0
12 4 2 0
13 4 2 0
14 4 2 0
15 4 2 0
16 4 2 0
17 4 2 0
I'd like it to be in a parent, child, child, child, ... order. So that all the children of a parent are shown before the next parent is started. Like this:
Parent
|
--- child L1
|
---- child L2
|
----- child L2
|
---- child L3
|
----- child L2
|
---- child L1
It seems like I should be able to accomplish this using ROW_NUMBER(), but I've been messing with it for a good half hour with no luck. Do I have to do this in a loop?