Solved

CTE organizational hierarchy needs base attached to recursive records.

Posted on 2011-09-15
2
228 Views
Last Modified: 2012-06-22
I've attached code regarding a CTE which recursively begins with a small set of head supervisors and pulls back the hierarchy level and employees reporting to them throughout the hierarchy. What I would like to do is attach each reporting employee to each one of the beginning/base set of head supervisors. How may I complete this task?
--Recursive Query
WITH EVP_Hierarchy (EmployeeNo, EmployeeName, JobCode, JobTitle, DateHired, AdjustedHireDate, DateTermed, 
						TermCode, TermDesc, SupervisorID, Supervisor, DeptNo, Department, HierarchyLevel) 
AS
(
--Base Query
SELECT	TOP (100) PERCENT lawson_temp.employee_nbr AS EmployeeNo,
		lawson_temp.first_name + ' ' + lawson_temp.last_name AS EmployeeName,
		lawson_temp.job_code AS JobCode,
		lawson_temp.job_title AS JobTitle,
		lawson_temp.date_hire AS DateHired,
		lawson_temp.adj_hire_date AS AdjustedHireDate,
		lawson_temp.term_date AS DateTermed,
		lawson_temp.term_code AS TermCode,
		lawson_temp.term_descr AS TermDesc,
		lawson_temp.supervisor_id AS SupervisorID,
		lawson_temp.supervisor_name AS Supervisor,
		lawson_temp.dept_nbr AS DeptNo,
		lawson_temp.dept_name AS Department,
		0 AS Hierarchy
FROM	lawson_temp
WHERE	(lawson_temp.term_date IS NULL) AND 
		(lawson_temp.employee_nbr IN (SELECT employee_nbr FROM lawson_temp WHERE (job_title = 'Executive Vice President')))
ORDER BY	lawson_temp.employee_nbr


UNION ALL

--Recursive Step
SELECT	lawson_temp.employee_nbr AS EmployeeNo,
		lawson_temp.first_name + ' ' + lawson_temp.last_name AS EmployeeName,
		lawson_temp.job_code AS JobCode,
		lawson_temp.job_title AS JobTitle,
		lawson_temp.date_hire AS DateHired,
		lawson_temp.adj_hire_date AS AdjustedHireDate,
		lawson_temp.term_date AS DateTermed,
		lawson_temp.term_code AS TermCode,
		lawson_temp.term_descr AS TermDesc,
		lawson_temp.supervisor_id AS SupervisorID,
		lawson_temp.supervisor_name AS Supervisor,
		lawson_temp.dept_nbr AS DeptNo,
		lawson_temp.dept_name AS Department,
		EVP_Hierarchy.HierarchyLevel + 1 AS HierarchyLevel
FROM	(SELECT	TOP (100) PERCENT supervisor_id AS SupervisorID, 
				employee_nbr AS EmployeeNo 
		FROM	lawson_temp 
		WHERE	(supervisor_id IS NOT NULL) AND (lawson_temp.term_date IS NULL) 
		ORDER BY	supervisor_id, 
					EmployeeNo) AS Supervisors
		INNER JOIN lawson_temp ON Supervisors.EmployeeNo = lawson_temp.employee_nbr
		INNER JOIN EVP_Hierarchy ON Supervisors.SupervisorID = EVP_Hierarchy.EmployeeNo
WHERE	(lawson_temp.term_date IS NULL)
)
--Final Select
SELECT	TOP (100) PERCENT HierarchyLevel,
		EmployeeNo, 
		EmployeeName, 
		JobCode, 
		JobTitle, 
		DateHired, 
		AdjustedHireDate, 
		SupervisorID, 
		Supervisor, 
		DeptNo, 
		Department
FROM	EVP_Hierarchy

Open in new window

0
Comment
Question by:Levi Martin
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36546626
HI.

Do you mean that you want to persist the top-level Supervisor throughout?

If so, amend your CTE definition to:

WITH EVP_Hierarchy (EVPEmployeeNo, EmployeeNo, ... )

Then for the first level, repeat the EmployeeNo:

SELECT      lawson_temp.employee_nbr AS EVPEmployeeNo, lawson_temp.employee_nbr AS EmployeeNo

Then in the recursive step, you can just keep pulling EVPEmployeeNo.

SELECT      EVP_Hierarchy.EVPEmployeeNo, lawson_temp.employee_nbr AS EmployeeNo

Hope that helps!

Kevin

P.S. I would remove the ORDER BY in the CTE and the TOP (100) PERCENT throughout. Do the ordering in the final selection.
0
 

Author Closing Comment

by:Levi Martin
ID: 36550167
Solution was complete, accurate, and informative. The answer was staring right back at me and is a lesson I will definitely carry with for further programming. Thank you so much!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
query execution hang 5 31
Sql Server group by 10 29
Tsql query 6 22
SQL Query assistance 16 26
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question