Click here to monitor SSC
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
Grasshopper
Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)Grasshopper (21 reputation)

Group: General Forum Members
Points: 21 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
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

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

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

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

Group: General Forum Members
Points: 10342 Visits: 9606
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