如何使用分层查询获取所有经理的列表

时间:2018-09-07 09:25:00

标签: oracle hierarchical-query

我想列出属于我的经理的员工列表。 看看下面的查询:

SELECT   SYS_CONNECT_BY_PATH (username, ':') AS "Liste Membres",LEVEL
  FROM   employees
 WHERE   CONNECT_BY_ISLEAF = 1
         AND username = '150') -- My_code
START WITH   manager IS NULL
CONNECT BY   PRIOR username = manager 

此查询的结果是:

:1:20:120:150

我想要得到的结果:

:1:20:120:

1 个答案:

答案 0 :(得分:1)

将其更改为SYS_CONNECT_BY_PATH (manager, ':'),然后删除附加到根级别:管理器的前导NULL(如果需要尾随:,则添加{{ 1}}):

SQL Fiddle

Oracle 11g R2架构设置

|| ':'

查询1

CREATE TABLE EMPLOYEES ( manager, username ) AS
  SELECT NULL, 1 FROM DUAL UNION ALL
  SELECT 1, 20 FROM DUAL UNION ALL
  SELECT 20, 120 FROM DUAL UNION ALL
  SELECT 120, 150 FROM DUAL;

Results

SELECT   SUBSTR( SYS_CONNECT_BY_PATH (manager, ':'), 2 ) AS "Liste Membres",
         LEVEL
FROM     employees
WHERE    CONNECT_BY_ISLEAF = 1
AND      username = '150' -- My_code
START WITH   manager IS NULL
CONNECT BY   PRIOR username = manager