I have a table with columns(ID,ParentId,name etc). the parentId can be null. I want to display results in such a way that first it displays parent and in next line it should display its child. How can i order in that way? I am using Microsoft Sql Server
Sample data:
ID cltName parentId
----------- -------------------------------------------------- -----------
1 a NULL
2 b NULL
3 c NULL
4 d NULL
5 e NULL
6 f NULL
7 g 6
8 h NULL
9 i 3
10 k NULL
11 yyk NULL
12 krr NULL
13 krre 12
The expected results are:
ID cltName parentId
----------- -------------------------------------------------- -----------
6 f NULL
7 g 6
3 c NULL
9 i 3
12 krr NULL
13 krre 12