Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this table i have a employee details.

EmpId EmpName ReportID
101 User1 201
102 User2 401
201 User3 301
301 User4 null

If I select the employeeID 101,i need to get who is the report id for the employeeid 101 and who is the report id for the employeeid 201 and who is the report id for the employeeid 301

I need the result should be like:

101 User1 201
201 User3 301
301 User4 null
Posted
Updated 7-Oct-13 21:46pm
v6
Comments
Stephen Hewison 8-Oct-13 3:39am    
You havent really explained your selection criteria. Can you elaborate on "something like"?
Gopijack89 8-Oct-13 4:03am    
In this table i have a employee details.

EmpId EmpName ReportID
101 User1 201
102 User2 401
201 User3 301
301 User4 null

If I select the employeeID 101,i need to get who is the report id for the employeeid 101 and who is the report id for the employeeid 201 and who is the report id for the employeeid 301

For example: select * from employee where empid=101

I need the result should be like:

101 User1 201
201 User3 301
301 User4 null
ridoy 8-Oct-13 4:04am    
check your solution below.
Thanks7872 8-Oct-13 3:39am    
Not clear at all. Elaborate your question.

try Some thing like this...
SQL
Create Table #Employee
	(
	 EmpId Int,
	 EmpName Nvarchar(40),
	 ReportID Int
	)

Insert into #Employee
Values(101,'User1',201),(102,'User2',401),(201,'User3',301),(301,'User4',null)

;with cte as
 ( 
   Select EmpId,EmpName,ReportID From #Employee Where EmpId =101-- Give EmpId Here
   Union all
   Select e.EmpId,e.EmpName,e.ReportID
   From #Employee e 
   Inner join cte c on c.ReportID = e.EmpId 
 )

 Select * From cte

Drop Table #Employee

Output:
EmpId	EmpName	ReportID
101	User1	201
201	User3	301
301	User4	NULL
 
Share this answer
 
v2
Comments
Rakshith Kumar 16-Oct-13 2:22am    
perfect
Select * From TableName Where EmpId <> 102
 
Share this answer
 
Comments
Rakshith Kumar 16-Oct-13 2:23am    
this may not work for all the criterion. This does not meet upto the requirement

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