sql - Need all data from table -


this question has answer here:

please help.

i have 1 table called employee.

i want record table self join parentid

record like

parent
b child of a
c child of a
d child of c
f child of b
e parent
g child of e
h child of g

if put self join , put record parent , a,b,c not d , f

i want record parent a,b,c,d,e .

you'll need use recursive cte if there isn't set number of parent/child levels. assuming you're using sql server 2005 or greater, should you're looking for:

with cte (   select id,      id parentid   employee   parentid null   union   select e.id,      c.parentid   employee e     join cte c on e.parentid = c.id   ) select id  cte parentid = 'a' 

sql fiddle demo

btw -- results in a, b, c, d, , f -- not e. assume typo in post.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -