Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
Please help me in the below requirement.

I have a table in which two columns will be there

ChildColumn -> ParentColumn
---------------------------
100 -> 101
100 -> 102
101 -> 103
102 -> 104
102 -> 105
103 -> 106
107 -> 108

I want to take the ParentColumn value of 100. Also the inter-dependencies of the parent column values too.Meaning for 100 the direct parent value is 101 and 102. Since 101 and 102 are depending on 103, 104 and 105 finally 103 depends on 106. Hence I wanted the output as below.

Output
------
101
102
103
104
105
106
Basically I wanted an output of dependency tree. Please help me in this.

Thanks & Regards,
Mathi
Posted
Updated 23-Mar-14 22:15pm
v2

This is exactly what recursive CTE is for:

SQL
with cte as
(
  select * from mytable where child = 100
  
  union all
  
  select t.* from mytable t
  inner join cte on cte.parent = t.child
)
select parent from cte
 
Share this answer
 
Comments
Maciej Los 24-Mar-14 19:21pm    
Exactly!
+5!
Mathi2code 25-Mar-14 1:31am    
Thanks +5.
 
Share this answer
 
Comments
Mathi2code 25-Mar-14 1:21am    
Thanks Maciej
Maciej Los 25-Mar-14 2:50am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900