[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

CTE organizational hierarchy needs base attached to recursive records.

Posted on 2011-09-15
2
Medium Priority
?
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

656 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