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

Hierarchy challenge: employee reports-to info, use an employee's manager if the immediate supervisor position is empty.

$
0
0

My experience level is not that high with SQL so please be gentle...

I am obtaining info for an employee including the employee's supervisor.  This is pretty straightforward except that sometimes the supervisor position is vacant.  In that case the employee's next level of management should be used for the reports to data.  

Order of Reports_To precedence: If a supervisor exists use their employee id, if they don't exist use the manager's employee id, if neither exist bypass the record.

Our tables include a Position Data and a Posn Incumbent table.  The Position Data table stores the reports to structure and the Posn Incumbent table lists the employee number for each position if one exists.  I can list the information but I'm not sure how to aggregate the results into one "reports to" column.

This is what I would like:

POSITION_NBR EMP        REPORTS_TO REPORTS_TO_EMP

------------ ----------- ---------- --------

2           10000002    1         10000001

3           10000003    1         10000001

4           10000004    2         10000002

5           NULL       3          10000003

6           10000006    4         10000004

7           NULL       4          10000004

8           10000008    3         10000003

9           10000009    8         10000004

Note the bolded entries under the Reports To section for Position Nbr 8 and 9.  Position Nbr 8 and 9 report to Position Nbr 5 and 7 respectively but these were vacant (no EMP) so the next level up was used.  (See next report for actual data.)

This what I am producing so far: 

POSITION_NBR EMP        REPORTS_TO SUP EMP     REPORTS_TO MNGR EMP

------------ ----------- ---------- ----------- ---------- -----------

2           10000002    1         10000001    NULL      NULL

3           10000003    1         10000001    NULL      NULL

4           10000004    2         10000002    1         10000001

5           NULL       3          10000003   1          10000001

6           10000006    4         10000004    2         10000002

7           NULL       4          10000004   2          10000002

8           10000008    5         NULL       3          10000003

9           10000009    7         NULL       4          10000004

The following test code can be used to produce the display directly above:

CREATE TABLE #My_POSITION_DATA
(
	POSITION_NBR nvarchar(8) NOT NULL,
	REPORTS_TO nvarchar(8) NULL
 CONSTRAINT PK_POSITION_NBR PRIMARY KEY CLUSTERED (POSITION_NBR ASC) );

CREATE TABLE #My_POSN_INCUMBENT
(
	POSITION_NBR nvarchar(8) NOT NULL,
	EMPLID nvarchar(11)  NOT NULL
 CONSTRAINT PK_POSITION_NBR2 PRIMARY KEY CLUSTERED (POSITION_NBR ASC) );
 
-- Populate the table with values.
INSERT INTO #My_POSITION_DATA VALUES 
 (1, NULL)
,(2, 1)
,(3, 1)
,(4, 2)
,(5, 3)
,(6, 4)
,(7, 4)
,(8, 5)
,(9, 7);

INSERT INTO #My_POSN_INCUMBENT VALUES 
 (1, 10000001)
,(2, 10000002)
,(3, 10000003)
,(4, 10000004)
--,(5, 10000005)
,(6, 10000006)
--,(7, 10000007)
,(8, 10000008)
,(9, 10000009);

select * 
from #My_POSITION_DATA;

select * 
from #My_POSN_INCUMBENT;

SELECT E.POSITION_NBR, EE.EMPLID AS 'EMP', E.REPORTS_TO
, FF.EMPLID AS 'SUP EMP', F.REPORTS_TO
, GG.EMPLID AS 'MNGR EMP'
FROM #My_POSITION_DATA E
INNER JOIN #My_POSITION_DATA F
ON E.REPORTS_TO = F.POSITION_NBR 
LEFT OUTER JOIN #My_POSN_INCUMBENT EE
 ON E.POSITION_NBR = EE.POSITION_NBR 
LEFT OUTER JOIN #My_POSN_INCUMBENT FF
 ON F.POSITION_NBR = FF.POSITION_NBR 
LEFT OUTER JOIN #My_POSN_INCUMBENT GG
 ON F.REPORTS_TO = GG.POSITION_NBR; 
drop table #My_POSITION_DATA;
drop table #My_POSN_INCUMBENT;

So, in summary how do I use the use the manager's employee id if the supervisor's employee id is blank?

Another question I had was if a CTE could be used to simplify the code a bit. 

Thanks in advance for any assistance you can provide,

Steve



Viewing all articles
Browse latest Browse all 23857

Trending Articles



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