SQL Server circular data query -
SQL Server circular data query -
i have simple table of 3 records below:
childid | parentid | name -------------------------- 1 | null | abc 2 | null | def 3 | 1 | ghi
what need query lists out name , parent name. in case of null should homecoming no parent, of sort:
childid | name | parent_name -------------------------------- 1 | abc | no parent 2 | def | no parent 3 | ghi | abc
since i'm new sql server i'm not sure how approach question. have tried search in vain. comments appreciated
a simple left outer join
job. seek this
create table #tttt ( childid int, parentid int, name varchar(50) ) insert #tttt values (1,null,'abc'), (2,null,'def'), (3,1,'ghi') select a.childid, a.name, isnull(b.name, 'no parent') parent_name #tttt left bring together #tttt b on b.childid = a.parentid
output
childid name parent_name ------ ---- ----------- 1 abc no parent 2 def no parent 3 ghi abc
sql sql-server
Comments
Post a Comment