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
 Query help

Author  Topic 

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2010-06-21 : 04:42:54

I have a table with name Users with 5 fields. Id (int), UserPF (varchar), UserName (varchar), ManagerPF (varhcar),
ManagerName (varchar)

Id ---- UserPF ---- UserName ---- ManagerPF ---- ManagerName
1 -------123 --------shaji---------456------------Alex
2 -------786 --------Raheem--------456------------Alex
3 -------456 --------Alex----------896------------Kudur
4 -------896 --------Kudur---------900------------Riyalat
5 -------900 --------Riyalat-------1000-----------Moby


I used a hierarchy here to understand who are the managers of UserName :shaji.
so,
I want to write a query to get who are the managers of UserName: shaji
From above table you can see that shaji manager is Alex and Alex manager is Kudur and Kudur manager is Riyalat and so on ...

How to get a generalized query for this request


Thanks in advance,
regards


SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-06-21 : 05:13:01
Use a recursive CTE (Common Table Expression). There are hundreds of examples on this site.



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

shajimanjeri
Posting Yak Master

179 Posts

Posted - 2010-06-21 : 05:28:57
Thanks Peso,

But can you provide an example kindly with the data which I provided. Because I am not familiar with recursive CTE

regards
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2010-06-21 : 05:50:23
Well, then this is an excellent opportunity for you to learn something new!
Or did you expect us to do your homework?

See this topic for more learning about recursive CTE
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=89335



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

- Advertisement -