Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How to display a multi-level hierarchy?

$
0
0

I have two tables: EmployeeMaster and EmployeeDetails. The schema of both are as below:
Schema

Sample data in both tables can be seen via these links. Because of the restriction of uploading only two images, I have added these images as links:

EmployeeMaster Table: http://i.imgbox.com/abd7DlrU.png

EmployeeDetails Table: http://i.imgbox.com/adon6s7g.png

I want to generate the hierarchy using EmployeeDetails table primarily. This table contains a column named: Manager. The EmployeeId of the Manager needs to be picked from the table EmployeeMaster table.

This is how the hierarchy needs to be formed. An EmployeeId is passed as a parameter to a stored procedure. The two supervisors of this Employee needs to be picked and 10 employees below this employee in seniority needs to be picked.

For instance, I pass the EmployeeId of Josh.Berkus to the stored procedure. The stored procedure query should return hierarchy as below:
Hierarchy

I want the final output in this format:

Employee_Id ..... Manager_Id
-----------               ------------

Please note that Manager_Id is the EmployeeId of Manager.

I tried using a CTE with UNION ALL query, but not able to get it correctly.



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>