Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
declare @teamLead as int
select @teamLead=Report_To from TM_USER where TM_UserID=106
(select distinct emp.TM_UserID,emp.FullName,TL.FullName as TeamLead from TM_User EMP
join TM_User TL on emp.Report_To=tl.TM_UserID where ( emp.Report_To=@teamLead or emp.TM_UserID=@teamLead ))
Posted
Updated 5-Sep-13 19:47pm
v3
Comments
Member 10256268 6-Sep-13 1:50am    
i am getting result like this TM_UserID FullName TeamLead
13 Deepti Injeti Subhash Goud
103 Uma Pandrinki Deepti Injeti
106 Chandu Devathi Deepti Injeti
142 Lakshmidevi Banyala Deepti Injeti
401 Alekhya Katuri Deepti Injeti
but this is also correct if change userid as 13 other team is displaying i dont want that sme team display if i give any useid in the above result.
Member 10256268 6-Sep-13 2:21am    
nobody knw the answer for this question

1 solution

Even the Team Lead will also report to some other team lead so complexity arises.. and based on condition we have to select team... u have to write the cond'n or logic to check either we have to display the employees that report to that particular Team lead(Lower Hirearchy) or employees that are working under teamlead's team lead(Next Upper Hirearchy)....
U can try some thing like this...
SQL
If Exists(select 1 from TM_USER where Report_To=@EmployeeId)--if the employee is team lead
 -- your logic here either higher hirearchy or lower hirearchy
Else -- if he is not teamlead
-- Logic here

SQL
Select Emp.TM_UserID,Emp.FullName as EmpName,TL.FullName as TeamLead
From TM_USER Tm
Inner join TM_USER T1 on Tm.Report_To=T1.TM_USERID
Left Join TM_USER Emp on Emp.Report_To=T1.TM_USERID or Emp.TM_UserID=T1.TM_USERID
Inner join TM_USER TL on TL.TM_UserID=Emp.Report_To
Where Tm.TM_UserID =106-- 106 is employee id -- this is for employee

Select EMP.TM_UserID,EMP.FullName as Empname,TL.FullName as TeamLead
From TM_USER T1
Inner join TM_USER EMP on T1.TM_UserID=EMP.Report_To or T1.TM_UserID=EMP.TM_userID
Inner join TM_USER TL on EMP.Report_To=TL.TM_UserID
Where T1.TM_UserID =13 -- this is for team lead
 
Share this answer
 
v5
Comments
Member 10256268 6-Sep-13 3:59am    
display employes who report to the user id given if user id given is team lead , else display the employes who report to the given user id's team lead

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