SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to arrange Employee manager Hierarchy tree in sql server .


How to arrange Employee manager Hierarchy tree in sql server .

Author
Message
$agar
$agar
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 53
create table #Sample_emp
(
Ename varchar(50),
EmployeeId int,
ManagerId int
)

insert into #Sample_emp values ('Faisal Husain',11,NULl)
insert into #Sample_emp values ('Deepak Zambre',12,11)
insert into #Sample_emp values ('Milind joshi',13,12)
insert into #Sample_emp values ('Mitesh Oswal',14,13)
insert into #Sample_emp values ('Saket verma',15,13)
insert into #Sample_emp values ('Sagar gadwe',16,13)
insert into #Sample_emp values ('Vinayak Kulkarni',17,13)

select * from #Sample_emp




WITH Asurion_mgr AS (
SELECT

e1.EmployeeId AS [employeeID]
, e1.EmployeeId AS [NewEMPID]
, e1.managerId AS [managerId]
, 0 AS [level]
, CAST(e1.Ename AS VARCHAR(MAX)) AS [Manager Hierarchy]
FROM
#Sample_emp e1


UNION ALL

SELECT

Am.[employeeId]
, e2.EmployeeId AS [EmployeeId]
, e2.managerID AS [managerId]
, Am.[level] + 1 AS [level]
, CAST(Am.[Manager Hierarchy] + ' - ' + e2.[Ename] AS VARCHAR(MAX)) AS [Manager Hierarchy2]
FROM
Asurion_mgr Am
JOIN #Sample_emp e2
ON e2.EmployeeId = Am.[managerId]
WHERE
e2.EmployeeId <> Am.[employeeID]

)
SELECT [employeeID],[Manager Hierarchy] FROM Asurion_mgr where managerId is null
sqlnaive
sqlnaive
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4383 Visits: 2774
Is it a question ? I see that you are getting what is required form the query.
$agar
$agar
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 53
Its a solution..:-)

if you want to add more from your side please add it...
Ed Wagner
Ed Wagner
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17428 Visits: 10100
Alas, if only we had "START WITH...CONNECT BY" in our SELECT statement.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search