我需要使用 Oracle SQL 获取经理的直接和间接报告,最高可达 3 级。
我正在使用 Oracle 的分层查询来获取报告。但我得到了一些奇怪的结果。
假设我有一个名叫 John 的经理。安妮向约翰报告,拉詹向安妮报告。
现在,如果我为 John 运行报告,那么如果 John 和向 Anne 和 Rajan 报告的报告,我应该得到所有的直接报告。拉詹有 5 名直接下属。
但是当我为 John 运行报告时,我只收到一份关于 Rajan 的报告。如果我为 Rajan 运行报告,则会获得 5 份报告。
这也发生在安妮身上。
并非所有的报告都列出了安妮在竞选约翰时的情况。
我没有得到这个问题。
这是我的代码:
SELECT DISTINCT PAPF.PERSON_NUMBER AS "EMPLOYEE_ID",PPNF.FULL_NAME AS "EMPLOYEE_NAME",PAAM.MANAGER_FLAG AS "MANAGER",
(SELECT PAPF1.PERSON_NUMBER FROM FUSION.PER_ALL_PEOPLE_F PAPF1,FUSION.PER_ASSIGNMENT_SUPERVISORS_F PASFWHERE PASF.MANAGER_ID = PAPF1.PERSON_ID
AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PASF.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PASF.EFFECTIVE_END_DATE, 'YYYY-MM-DD')
AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PAPF1.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PAPF1.EFFECTIVE_END_DATE, 'YYYY-MM-DD')
AND PASF.MANAGER_TYPE = 'LINE_MANAGER'
AND PAAM.ASSIGNMENT_ID = PASF.ASSIGNMENT_ID
AND PAAM.PERSON_ID=PASF.PERSON_ID) AS "SUPERVISOR_ID",
(SELECT PPNF1.FULL_NAME FROM PER_PERSON_NAMES_F PPNF1, PER_ASSIGNMENT_SUPERVISORS_F PASF WHERE PPNF1.PERSON_ID=PASF.MANAGER_ID AND PASF.MANAGER_TYPE= 'LINE_MANAGER' AND PAAM.ASSIGNMENT_ID=PASF.ASSIGNMENT_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PASF.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PASF.EFFECTIVE_END_DATE, 'YYYY-MM-DD') AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PPNF1.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PPNF1.EFFECTIVE_END_DATE, 'YYYY-MM-DD') AND PPNF1.NAME_TYPE='GLOBAL')AS "SUPERVISOR_NAME", TO_CHAR(PPS.ORIGINAL_DATE_OF_HIRE,'MM/DD/YYYY') AS "START_DATE",
(SELECT HAPF.ATTRIBUTE2 FROM HR_ALL_POSITIONS_F HAPF WHERE PAAM.POSITION_ID=HAPF.POSITION_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(HAPF.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(HAPF.EFFECTIVE_END_DATE, 'YYYY-MM-DD'))AS "OPERATING BU",
(SELECT HLA.LOCATION_NAME FROM HR_LOCATIONS_ALL HLA WHERE PAAM.LOCATION_ID=HLA.LOCATION_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(HLA.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(HLA.EFFECTIVE_END_DATE, 'YYYY-MM-DD'))AS "LOCATION_DESCRIPTION", PAAM.ORGANIZATION_ID AS "DEPARTMENT_ID",
(SELECT HOFT.NAME FROM HR_ALL_ORGANIZATION_UNITS HOFT WHERE PAAM.ORGANIZATION_ID=HOFT.ORGANIZATION_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(HOFT.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(HOFT.EFFECTIVE_END_DATE, 'YYYY-MM-DD'))AS "DEPARTMENT_DESCRIPTION",
(SELECT HAPF.FTE FROM HR_ALL_POSITIONS_F HAPF WHERE PAAM.POSITION_ID=HAPF.POSITION_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(HAPF.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(HAPF.EFFECTIVE_END_DATE, 'YYYY-MM-DD'))AS "HEADCOUNT",
(SELECT PG.NAME FROM PER_GRADES PG WHERE PAAM.GRADE_ID=PG.GRADE_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PG.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PG.EFFECTIVE_END_DATE, 'YYYY-MM-DD'))AS "BAND",
(SELECT HAPFT.NAME FROM HR_ALL_POSITIONS_F_TL HAPFT WHERE PAAM.POSITION_ID=HAPFT.POSITION_ID AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(HAPFT.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(HAPFT.EFFECTIVE_END_DATE, 'YYYY-MM-DD'))AS "BUSINESS_TITLE"
FROM PER_ALL_PEOPLE_F PAPF,PER_ALL_ASSIGNMENTS_M PAAM,
PER_PERIODS_OF_SERVICE PPS,PER_PERSON_NAMES_F PPNF
WHERE PAPF.PERSON_ID=PAAM.PERSON_ID
AND PAAM.PERSON_ID=PPS.PERSON_ID
AND PAAM.PERIOD_OF_SERVICE_ID=PPS.PERIOD_OF_SERVICE_ID
AND PAAM.PERSON_ID=PPNF.PERSON_ID(+)
AND PPNF.NAME_TYPE='GLOBAL'
AND PAAM.SYSTEM_PERSON_TYPE='EMP'
AND PAAM.ASSIGNMENT_TYPE = 'E'
AND PAAM.PRIMARY_FLAG='Y'
AND PAAM.PRIMARY_ASSIGNMENT_FLAG='Y'
AND PAAM.PRIMARY_WORK_RELATION_FLAG='Y'
AND PAAM.PERSON_ID IN
(SELECT PERSON_ID FROM
(SELECT DISTINCT PASF.PERSON_ID,PASF.MANAGER_ID
FROM PER_ASSIGNMENT_SUPERVISORS_F PASF)TEMP
START WITH PERSON_ID in(:manager_name)
connect by prior PERSON_ID=MANAGER_ID and level <='3')
AND PAAM.ASSIGNMENT_STATUS_TYPE IN ( 'ACTIVE', DECODE(:INCLUDE_INACTIVE, 'Y','INACTIVE','ACTIVE')) AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PAAM.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PAAM.EFFECTIVE_END_DATE, 'YYYY-MM-DD') AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PPNF.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PPNF.EFFECTIVE_END_DATE, 'YYYY-MM-DD')AND NVL( TO_CHAR (:AS_ON_DATE, 'YYYY-MM-DD'), to_char( sysdate, 'YYYY-MM-DD')) BETWEEN to_char(PAPF.EFFECTIVE_START_DATE, 'YYYY-MM-DD') AND to_char(PAPF.EFFECTIVE_END_DATE, 'YYYY-MM-DD')
我试过搜索但找不到任何东西..请帮忙。