Thursday, December 5, 2013

Dynamic CTE Prompt table

I was asked to create a prompt record for the Recruiter prompt on our Job Requisitions to make it easier for our Talent Acquisition team. I analyzed the data and could not filter it down to a couple of Jobcodes, then I realized 90% of the associates that reported to the head of Talent Acquisition were recruiters. So I got to thinking, that a CTE (Common Table Expresion) would work really well in this scenario.  So I created a field on our custom installation record that identified the head of Talent Acquisition.  Then I wrote this SQL.

WITH REPORTS ( OPRID, NAME, ROLEUSER_SUPR) AS (
 SELECT A.roleuser
 , A.descr
 , A.ROLEUSER_SUPR
  FROM PS_ROLEXLATOPR A
  , PS_ZZ_INSTALLATION I
 WHERE A.OPRID = I.ROLEUSER_SUPR
  UNION ALL
 SELECT A.OPRID
 , A.DESCR
 , A.ROLEUSER_SUPR
  FROM PS_ROLEXLATOPR A
  , REPORTS X
 WHERE  A.ROLEUSER_SUPR = X.OPRID
   AND A.OPRID > ' ')
 SELECT *
  FROM REPORTS

Here is my record Definition:


Now this assumes that you are keeping the roleuser_supr updated.  But if you are keeping it updated then you can write a recursive CTE to return all direct and indirect reports to a position.




No comments:

Post a Comment