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
 SQL Server 2012 Forums
 Transact-SQL (2012)
 i have following table with data

Author  Topic 

asifbhura
Posting Yak Master

165 Posts

Posted - 2014-03-16 : 08:18:15
I have following table with data


CREATE TABLE EMPLOYEE
(
EmployeeId int,
ManagerId int,
EmployeeName varchar(10)
)
INSERT EMPLOYEE SELECT 1,2,'Peter'
INSERT EMPLOYEE SELECT 2,3,'David'
INSERT EMPLOYEE SELECT 3,NULL,'Jane'
INSERT EMPLOYEE SELECT 4,2,'Mary'
INSERT EMPLOYEE SELECT 5,2,'Henry'


I want output like below,


Employee Manager
Peter David
David Jane
Mary David
Henry David


and

Employee Manager
Peter David
David Jane
Jane No Manager
Mary David
Henry David


please help me out

nagino
Yak Posting Veteran

75 Posts

Posted - 2014-03-16 : 20:20:00
[code]SELECT
E.EmployeeName Employee,
M.EmployeeName Manager
FROM EMPLOYEE E
INNER JOIN EMPLOYEE M
ON E.ManagerId = M.EmployeeID

SELECT
E.EmployeeName Employee,
ISNULL(M.EmployeeName, 'No Manager') Manager
FROM EMPLOYEE E
LEFT JOIN EMPLOYEE M
ON E.ManagerId = M.EmployeeID[/code]

-------------------------------------
From Japan
Sorry, my English ability is limited.
Go to Top of Page
   

- Advertisement -