“按优先顺序联系”忽略无父母的孩子

时间:2018-08-28 19:37:40

标签: sql oracle

我尝试使用此原始帖子how do I omit parent less nodes in an hierarchical (connect by) oracle query?中的解决方案。但是,我似乎仍然无法解决我的问题(除非使用此解决方案,我会丢失某些东西)。在该示例中,它仍然显示Emp Id 2 GM及其子级孩子(已更活跃)和Emp Id 3 PRJMGR及其子级(不再是PRJMGR,现在是GM)的组。

如果有人需要使用示例来跑步/玩游戏,我已经使用livesql.oracle.com构建了它:

DROP TABLE EMPLOYEE;
DROP TABLE EMPLOYEE_ASSIGNMENT;
DROP TABLE COMPANY_STRUCTURE_POSITION;
CREATE TABLE EMPLOYEE (EMPLOYEE_ID VARCHAR2(15 CHAR));
CREATE TABLE EMPLOYEE_ASSIGNMENT (EMPLOYEE_ID VARCHAR(15 CHAR),
                                  POSITION_CODE VARCHAR2(10 CHAR),
                                  VALID_FROM DATE,
                                  VALID_TO DATE);
CREATE TABLE COMPANY_STRUCTURE_POSITION (POSITION_CODE VARCHAR2(10 CHAR),
                                         SUPERVISOR_POSITION_CODE VARCHAR2(10 CHAR));
INSERT INTO EMPLOYEE VALUES ('1');
INSERT INTO EMPLOYEE VALUES ('2');
INSERT INTO EMPLOYEE VALUES ('3');
INSERT INTO EMPLOYEE VALUES ('4');
INSERT INTO EMPLOYEE VALUES ('5');          
INSERT INTO EMPLOYEE VALUES ('6');
INSERT INTO EMPLOYEE VALUES ('7');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('1', 'CEO', '01-JAN-18', '31-DEC-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('2', 'GM', '01-JAN-18', '30-JUN-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('3', 'GM', '01-JUL-18', '31-DEC-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('3', 'PRJMGR', '01-JAN-18', '30-JUN-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('4', 'PRJMGR', '01-JUL-18', '31-DEC-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('5', 'HR', '01-JAN-18', '31-DEC-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('6', 'PNT', '01-JAN-18', '31-DEC-18');
INSERT INTO EMPLOYEE_ASSIGNMENT VALUES ('7', 'WLD', '01-JAN-18', '31-DEC-18');
INSERT INTO COMPANY_STRUCTURE_POSITION VALUES ('CEO', '*');
INSERT INTO COMPANY_STRUCTURE_POSITION VALUES ('GM', 'CEO');
INSERT INTO COMPANY_STRUCTURE_POSITION VALUES ('PRJMGR', 'GM');
INSERT INTO COMPANY_STRUCTURE_POSITION VALUES ('HR', 'CEO');
INSERT INTO COMPANY_STRUCTURE_POSITION VALUES ('PNT', 'PRJMGR');
INSERT INTO COMPANY_STRUCTURE_POSITION VALUES ('WLD', 'PRJMGR');
SELECT sys_connect_by_path(e.employee_id, '->') EMPLOYEE_PATH, 
       sys_connect_by_path(csp.position_code, '->') POSITION_CODE_PATH, 
       level,  
       e.*, 
       ea.*, 
       csp.* 
FROM employee e 
INNER JOIN employee_assignment ea ON ea.employee_id = e.employee_id 
INNER JOIN company_structure_position csp ON csp.position_code = ea.position_code 
WHERE ea.valid_from <= TRUNC(sysdate) 
AND ea.valid_to >= TRUNC(sysdate) 
START WITH csp.supervisor_position_code = '*' 
AND ea.valid_from <= TRUNC(sysdate) 
AND ea.valid_to >= TRUNC(sysdate) 
CONNECT BY PRIOR csp.position_code = csp.supervisor_position_code

产生的SQL:

| EMPLOYEE_PATH | POSITION_CODE_PATH    | LEVEL | EMP_ID | EMP_ID | POS_CODE | VALID_FROM | VALID_TO  | POS_CODE | SUP_POS_CODE |
---------------------------------------------------------------------------------------------------------------------------------
| ->1           |->CEO                  |  1    |  1     |  1     |  CEO     | 01-JAN-18  | 31-DEC-18 | CEO      | *            |
| ->1->2->3->6  |->CEO->GM->PRJMGR->PNT |  4    |  6     |  6     |  PNT     | 01-JAN-18  | 31-DEC-18 | PNT      | PRJMGR       |
| ->1->2->3->7  |->CEO->GM->PRJMGR->WLD |  4    |  7     |  7     |  WLD     | 01-JAN-18  | 31-DEC-18 | WLD      | PRJMGR       |
| ->1->2->4     |->CEO->GM->PRJMGR      |  3    |  4     |  4     |  PRJMGR  | 01-JUL-18  | 31-DEC-18 | PRJMGR   | GM           |
| ->1->2->4->6  |->CEO->GM->PRJMGR->PNT |  4    |  6     |  6     |  PNT     | 01-JAN-18  | 31-DEC-18 | PNT      | PRJMGR       |
| ->1->2->4->7  |->CEO->GM->PRJMGR->WLD |  4    |  7     |  7     |  WLD     | 01-JAN-18  | 31-DEC-18 | WLD      | PRJMGR       |
| ->1->3        |->CEO->GM              |  2    |  3     |  3     |  GM      | 01-JUL-18  | 31-DEC-18 | GM       | CEO          |
| ->1->3->3->6  |->CEO->GM->PRJMGR->PNT |  4    |  6     |  6     |  PNT     | 01-JAN-18  | 31-DEC-18 | PNT      | PRJMGR       |
| ->1->3->3->7  |->CEO->GM->PRJMGR->WLD |  4    |  7     |  7     |  WLD     | 01-JAN-18  | 31-DEC-18 | WLD      | PRJMGR       |
| ->1->3->4     |->CEO->GM->PRJMGR      |  3    |  4     |  4     |  PRJMGR  | 01-JUL-18  | 31-DEC-18 | PRJMGR   | GM           | 
| ->1->3->4->6  |->CEO->GM->PRJMGR->PNT |  4    |  6     |  6     |  PNT     | 01-JAN-18  | 31-DEC-18 | PNT      | PRJMGR       |
| ->1->3->4->7  |->CEO->GM->PRJMGR->WLD |  4    |  7     |  7     |  WLD     | 01-JAN-18  | 31-DEC-18 | WLD      | PRJMGR       |
| ->1->5        |->CEO->HR              |  2    |  5     |  5     |  HR      | 01-JAN-18  | 31-DEC-18 | HR       | CEO          |  

感谢您的建议。

1 个答案:

答案 0 :(得分:0)

CONNECT BY子句中包含不通过非活动节点进行连接的条件。像这样:

CONNECT BY PRIOR csp.position_code = csp.supervisor_position_code
AND ea.valid_from <= TRUNC(sysdate) 
AND ea.valid_to >= TRUNC(sysdate)