Click here to Skip to main content
Sign Up to vote bad
good
See more: SQL2005SQLTSQL
Hi All,
I've a table whose schema and sample data are as follows:
 
EmpCode               ManagerCode
=========             =============
1                        1
2                        1
3                        2
4                        2
5                        3
6                        2
7                        3
8                        4
The name of the table is EMP
 
Now my task is to create a query which will give a hierarchy of manager against a particular EmpCode. Say for example for EmpCode 7 the output will be
 
EmpCode
=======
1
2
3
I've tried with self join but its giving only the immediate manager not the total hierarchy. It will be of great help if provided with any clue to achieve this result
Posted 28-Dec-10 23:36pm

Comments
Hiren Solanki - 29-Dec-10 6:30am
Reply : so it will return 1,2,3,4 ? do you want it to be different. ?
senguptaamlan - 29-Dec-10 6:33am
can be depending upon the empID...1,2,3,4 are not static data...they are given just for an example.
Hiren Solanki - 29-Dec-10 6:33am
I've got the problem actually, It needs some extra efforts.
Hiren Solanki - 31-Dec-10 0:51am
You could read the 'story behind' of that TIP, and you could vote also if you liked it.
Hiren Solanki - 30-Dec-10 8:21am
see my updated answer, I've written a complete trip for you on the otherside.

3 solutions

What about THIS
 
select distinct managerid from EMP where managerid <= (select managerid from EMP where empid=7)

 
You could try building Recursive CTE[^] for data hirarchy.
 
See my newly written TIP[^] only for you.
  Permalink  
Comments
Hiren Solanki - 29-Dec-10 6:32am
Okey I got the problem actually now, Let me try it.
senguptaamlan - 29-Dec-10 6:28am
if I give empID = 8 ?
senguptaamlan - 31-Dec-10 0:46am
@Hiren thanks man....
Hi,
 
create procedure testemp(@empid int)
as
begin
declare @temp int;
create table #temp (mgrid int,);
select @temp=mgrid  from empmgr  where empid =@empid;
insert into  #temp (mgrid)  values (@temp);
while (@temp != @empid)
begin
select @temp=mgrid , @empid =empid  from empmgr where empid =@temp;
insert into  #temp (mgrid)  values (@temp);
end
select * from #temp;
end
 
CREATE TABLE [dbo].[empmgr](
    [empid] [int] NULL,
    [mgrid] [int] NULL
) ON [PRIMARY]
  Permalink  
Comments
Marcus Kramer - 29-Dec-10 16:23pm
My vote of 1.... WTF!!!
Maybe the following article will help you :
http://www.sqlteam.com/article/more-trees-hierarchies-in-sql[^]
 
Cheers
  Permalink  
Comments
senguptaamlan - 29-Dec-10 5:58am
yeah provides a good solution, but breaks down my present DB Structure if I'm gonna use the depth, Lineage columns...
Dylan Morley - 29-Dec-10 5:54am
good article

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 545
1 Ron Beyer 316
2 Tadit Dash 233
3 samadhan_kshirsagar 229
4 OriginalGriff 198
0 Sergey Alexandrovich Kryukov 7,061
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,370
4 CPallini 3,034


Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 30 Dec 2010
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid