2
Reply

Parent Child SQL QUERY PROBLEM

Ask a question
ishan

ishan

14y
2.4k
1
I am using MS SQL server, I have this table
aid         ans        parent         
----------- ---------- -----------
1           a          0
2           b          1
3           c          1
4           d          0
5           e          0
6           f          4
7           g          4
8           h          1
9           i          5
10          j          5
11          k          0
12          l          6
13          m          2


I need this output
aid is primary key,ans in some text, parent is parent child relationship. parent value 0 means it is on head level, parent value 1 means its parent is having aid= 1, parent value 6 means its parent is having aid=6 and I hv to arrange in hierarchical order of parent child like as shown below. I need query for this, Plz help
aid            ans           parent
1               a               0
2               b               1
13              m               2
3               c               1
8               h               1
4               d               0
6               f               4
12              l               6
7               g               4
5               e               0
9               i               5
10              j               5
11              k               0


Answers (2)