Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need hierarchical query

Author  Topic 

ArpitNarula
Starting Member

16 Posts

Posted - 2011-01-31 : 23:30:38
Hi Guys,

Below is the scenerio:

Table A
id name parent id

1 a 2
2 b 3
3 c null
4 d 3
5 e 1

Need output like:
name parent name
a b
b c
c Null
d c
e a

Thanks in advance.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-02-01 : 01:28:05
SELECT t2.Name, t1.Name AS ParentName
FROM dbo.TableA AS t1
INNER JOIN dbo.TableA AS t2 ON t2.ID = t1.ParentID


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2011-02-01 : 01:37:03
Perhaps this would give the desired output

SELECT
t1.Name,t2.Name as parentName
FROM @TableA AS t1
Left JOIN @TableA AS t2 ON t1.ParentID = t2.ID
Go to Top of Page
   

- Advertisement -