Sunday, 15 May 2016

Find Employee- Supervisor Hierarchy in Oracle Apps


  SELECT papf.employee_number,           papf.person_id,
           papf.full_name,
           ppg.segment1 officer_flag, --executive flag for officers (Y is officer, N is not)
           NVL (spapf.full_name, 'NONE') supervisor_name,
           spapf.person_id supv_person_id,
           LEVEL
      FROM per_people_x papf,
           PER_PEOPLE_X SPAPF,
           per_assignments_x paaf,
           pay_people_groups ppg
     WHERE     papf.person_id = paaf.person_id
           AND sysdate BETWEEN papf.effective_start_date AND papf.effective_end_date
           AND sysdate BETWEEN paaf.effective_start_date AND paaf.effective_end_date
           and sysdate between SPAPF.EFFECTIVE_START_DATE and SPAPF.EFFECTIVE_END_DATE
           --AND xxx_person_type.is_employee(:p_eff_date, papf.person_id) = 'TRUE'
           AND paaf.assignment_type IN ('E', 'C')
           AND paaf.supervisor_id = spapf.person_id
           AND paaf.people_group_id = ppg.people_group_id
           AND paaf.primary_flag = 'Y'
START WITH papf.employee_number = :p_emp_num --emp num of employee or top level supervisor?
CONNECT BY PRIOR spapf.employee_number = papf.employee_number --AND LEVEL < :p_level
  ORDER BY LEVEL DESC
;

--------------------------------------------------------------------
OR

    SELECT LEVEL seq,
           e.person_id,
           e.grade_id,
           e.job_id,
           e.supervisor_id,
           e.employee_number,
           e.full_name
      FROM (SELECT DISTINCT paf.person_id,
                            paf.grade_id,
                            paf.job_id,
                            paf.supervisor_id,
                            ppf.employee_number,
                            ppf.full_name
              FROM    per_all_people_f ppf
                   LEFT JOIN
                      per_all_assignments_f paf
                   ON     ppf.person_id = paf.person_id
                      AND paf.person_id IS NOT NULL
                      AND (SYSDATE BETWEEN ppf.effective_start_date
                                       AND ppf.effective_end_date)
                      AND (SYSDATE BETWEEN paf.effective_start_date
                                       AND paf.effective_end_date)) e
CONNECT BY person_id = PRIOR supervisor_id
START WITH employee_number = :p_employee_number         --person_id = :P_PERSON_ID
  ORDER BY LEVEL
;

No comments:

Post a Comment