Click here to Skip to main content
Click here to Skip to main content

Building hierarchy using Recursive CTE

By , 5 Jan 2011
 

Introduction

Building hirarchy in the sense of hierarchy of manager, employee though both are an
employee of an enterprise but both are related or depend on hirarchical level.

Retrieved hierarchy can also be useful to display it properly in many of the controls
like Menu, Tree view and many more in the list.

Relation SQL doesn't handle hierarchical data quite efficiently than XML does. There are several different ways to handle hierarchical data, But the familiar one is an adjacency model and it looks like below. The data is actually stored in DB



EmployeeID
ManagerID
EmployeeName
1
0
Hiren Solanki
2
1
Gaurang Devluk
3
2
Matthew Dave
4
5
Dhaval Raninga
5
3
Altaf Khan
6
2
Virang Patel
7
3
Rajesh Thakur
8
4
Tusshar Surve

Now what if you want to find a hierarchy for an employee no. 8, with the following result:


EmployeeName
EmployeeID
ManagerID
Tusshar Surve
8
4
Dhaval Raninga
4
2
Gaurang Devluk
2
1
Hiren Solanki
1
0

Write your T-SQL query using Recursive CTE, like:

WITH CTE(EmployeeName,empcode,managercode) AS
(
SELECT EmployeeName,empcode,managercode from EMP where empcode=8
UNION ALL
SELECT e.EmployeeName,e.empcode,e.managercode
from EMP e 
INNER JOIN CTE c ON e.empcode = c.managercode
)
SELECT * FROM CTE

Query Explanation

With the first query in union all, it just simply retrieves record having employeeID 8 (one record), further second query using the result of the first query to join with and by joining again, it will loop until it finds that employee 1 does not have any manager to join with.

Story Behind

Let me tell you one interesting thing behind writing this trick is that I have questioned[^] once to solve this query and I was also curious to finding a solution for.

 
Thanks for your time, guys.

License

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

About the Author

Hiren solanki
Software Developer
India India
Member
He is a Smart IT devloper with Few years of Expeariance But having Great command on ASP.net,C#,SQL Query,SSRS,Crystal Reports
 
Apart from that He Loves multimedia work too, Master of Adobe photoshop, Illustrator, CSS , HTML and all things.
 
He is Currently working in Microsoft Dynamics CRM and Having Nice Expearince with CRM. CRM Rocks!!!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralUr tip got good vote, but my answer didn't :) just kiddingmembersenguptaamlan30 Dec '10 - 18:48 
Ur tip got good vote, but my answer didn't Smile | :) just kidding
GeneralReason for my vote of 5 this one is nicememberPranay Rana30 Dec '10 - 16:46 
Reason for my vote of 5
this one is nice
GeneralThanks Estys you liked it. and thanks for pointing me out on...memberHiren Solanki30 Dec '10 - 5:41 
Thanks Estys you liked it. and thanks for pointing me out on my mistake, It's corrected accordingly.
GeneralReason for my vote of 5 This is a great tip! Very useful to...memberEstys30 Dec '10 - 4:53 
Reason for my vote of 5
This is a great tip!
Very useful to me.
 
PS it's hierarchy, not hirarchy Smile | :)
GeneralThanks Manfred.memberHiren Solanki30 Dec '10 - 3:44 
Thanks Manfred.
GeneralReason for my vote of 5 Great tip!mvpManfred R. Bihy30 Dec '10 - 3:43 
Reason for my vote of 5
Great tip!
Generalvery nice vote of 5membervikas amin4 Jan '11 - 10:02 
very nice Hiren
GeneralRe: very nice vote of 5memberHiren Solanki4 Jan '11 - 17:53 
Thank you Vikas.
Regards,
Hiren.

GeneralA modest questionmemberManfred R. Bihy30 Dec '10 - 4:04 
I've playing around with the idea of publishing a Tip/Trick that came from one my answers. When I looked at the editor though I saw it was the same as we use for writing answers and it is a bit cumbersome especially if it is a longer text you want to enter with many code samples.
Since you have already been publishing for some time now, I'd like to know how you go about entering yours. Do you have a tip for me?
GeneralRe: A modest questionmemberHiren Solanki30 Dec '10 - 5:38 
Manfred R. Bihy wrote:
I've playing around with the idea of publishing a Tip/Trick that came from one my answers

 
Yes, You could always publish it if you think it's having some knowledge to share about and you think of getting good response.
 
Manfred R. Bihy wrote:
When I looked at the editor though I saw it was the same as we use for writing answers and it is a bit cumbersome especially if it is a longer text you want to enter with many code samples.

 
I didn't get actually on this point but It seems like you are probably asking about plagiarizing of your answer by another person. ?Unsure | :~
 
Manfred R. Bihy wrote:
Since you have already been publishing for some time now, I'd like to know how you go about entering yours. Do you have a tip for me?

 
Are you asking about some subject to post a TIP/TRICK on ? or probably you need some tip about writing TIP ?
Regards,
Hiren.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 5 Jan 2011
Article Copyright 2010 by Hiren solanki
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid