Monday, September 28, 2015

Query to Get Supervisor Hierarchy using Connect By Clause

SELECT per1.employee_number,
       per1.full_name,
       (    SELECT listagg (' Level:' || LEVEL || ' ' || per.full_name, CHR (10))
                      WITHIN GROUP (ORDER BY LEVEL)
              FROM per_assignments_x paaf, per_people_x per
             WHERE per.person_id = paaf.person_id
        START WITH paaf.person_id = paaf1.supervisor_id
        CONNECT BY paaf.person_id = PRIOR paaf.supervisor_id
                   AND paaf.supervisor_id IS NOT NULL)
          supervisor_levels
  FROM per_all_people_f per1, per_all_assignments_f paaf1
 WHERE     per1.person_id = paaf1.person_id
       AND paaf1.assignment_type = 'E'
       AND paaf1.primary_flag = 'Y'
       AND paaf1.assignment_status_type_id IN (1, 2)
       AND TRUNC (SYSDATE) BETWEEN per1.effective_start_date
                               AND per1.effective_end_date
       AND TRUNC (SYSDATE) BETWEEN paaf1.effective_start_date
                               AND paaf1.effective_end_date

--       AND per1.employee_number = 5925

2 comments: