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 2000 Forums
 SQL Server Development (2000)
 Hierarchy Employee Listing

Author  Topic 

emilly
Starting Member

4 Posts

Posted - 2010-10-11 : 16:54:40
I am trying create a SQL extract file that has the following layout below.

User UserBoss Level1 Level2 Level3 Level4 Level5
John John
Bob John John Bob
Tom Bob John Bob Tom
Doug Tom John Bob Tom Doug
Brad Doug John Bob Tom Doug Brad
Lee John John Lee
Sue Lee John Lee Sue

This listing doesn't have to be sorted in this order and anyone in the listing could have multiple people reporting to them. Any ideas?

Currently, I have a sql recursive query (plus case statements) working that ranks employees and then puts them in the levels hierarchy as described above. However, I can only get the level hierarchy to work for Level 1 & 2, or Level 2 & 3, Level 3 & 4, etc. I am wondering how to add on to this to complete the requested output. A co-worker has suggested creating a procedure or using a temp db? Please see my recursive query and case statements below. Also below are the output of this query.


WITH EmployeesCTE AS
( SELECT 0 AS Rank, EI.EmployeeNumber, EI.DisplayName, EI.ManagerEmployeeNumber, EI.ManagerName, EI.Company, EI.Division, EI.Department,
EI.JobTitle
FROM UltiPro.NoodleStream_EmployeeInfoForHierarchy EI
WHERE EI.ManagerEmployeeNumber = '999999'
UNION ALL
SELECT Rank + 1, EI.EmployeeNumber, EI.DisplayName, EI.ManagerEmployeeNumber, EI.ManagerName, EI.Company, EI.Division, EI.Department,
EI.JobTitle
FROM UltiPro.NoodleStream_EmployeeInfoForHierarchy EI
JOIN EmployeesCTE ON EI.ManagerEmployeeNumber = EmployeesCTE.EmployeeNumber
)
SELECT *,
CASE WHEN Rank = 1 THEN DisplayName
WHEN Rank = 2 THEN ManagerName END AS Level1,
CASE WHEN Rank = 2 THEN DisplayName
WHEN Rank = 3 THEN ManagerName END AS Level2,
CASE WHEN Rank = 3 THEN DisplayName
WHEN Rank = 4 THEN ManagerName END AS Level3,
CASE WHEN Rank = 4 THEN DisplayName
WHEN Rank = 5 THEN ManagerName END AS Level4,
CASE WHEN Rank = 5 THEN DisplayName
WHEN Rank = 6 THEN ManagerName END AS Level5,
CASE WHEN Rank = 6 THEN DisplayName
WHEN Rank = 7 THEN ManagerName END AS Level6,
CASE WHEN Rank = 7 THEN DisplayName END AS Level7
FROM EmployeesCTE




User UserBoss Level1 Level2 Level3 Level4 Level5
John John
Bob John John Bob
Tom Bob Bob Tom
Doug Tom Tom Doug
Brad Doug Doug Brad
Lee John John Lee
Sue Lee Lee Sue

I appreciate any help! Thanks!

emilly
Starting Member

4 Posts

Posted - 2010-10-11 : 17:00:07
I apologize, I left out part of my results I'm getting with the current query. The first column contains rank.

Rank User UserBoss Level1 Level2 Level3 Level4 Level5
1 John John
2 Bob John John Bob
3 Tom Bob John Bob Tom
4 Doug Tom John Bob Tom Doug
5 Brad Doug John Bob Tom Doug Brad
2 Lee John John Lee
3 Sue Lee John Lee Sue
Go to Top of Page
   

- Advertisement -