Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL query help needed!

Status
Not open for further replies.

vngpal

Programmer
Jan 14, 2004
35
0
0
US
I have a table Emp. Each employee is assigned to a manager and each manager is assigned to another manager above the employee. This level may go upto 4 or 5 levels. Some employees may not have a manager assigned to them (for instance test employees and also the department head). The data is stored in a table called Emp. Each employee has a primary key and the manager also has a primary key as an employee. The table is designed such a way that for each employee who is assigned a manager is idendified with the manager's primary key as (Foreign Key) for that employee row.
Emp_Pk Emp_MgrEmpFk EmpName
134 23 San
254 23 David
23 56 Frank
56 11 Rob
11 Null Jessica


In this example the employee Jessica is not associated with any manager. So I want to pull data showing Jessica at the top level and below is Rob and below Rob Frank and Below Frank I want to show San and David.

I need to write a sql query for this.

Can you guys help me out?

I am using MS SQL server 2003.

Thanks
Programmer
 
This a classic example of a self-join; check 'er out in BOL....

The early bird may get the worm, but the second mouse gets the cheese in the trap.
 
hold on a sec...

I am using MS SQL server 2003.

really?

where did you get a copy of that?

--------------------
Procrastinate Now!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top