Oracle SQL 查询让所有代理直接或间接向经理报告

Posted

技术标签:

【中文标题】Oracle SQL 查询让所有代理直接或间接向经理报告【英文标题】:Oracle SQL query to get all agents reporting directy or indirectly to a manager 【发布时间】:2019-02-06 14:53:00 【问题描述】:

我有一个以下格式的员工层次结构表

EMPLOYEE_NO  TEAM_ROLE          MANAGER_EMP_NO
10001        Functional_Manager 
123          Center_Manager     10001
10           Team_Manager       123
11           Team_Manager       123
12           Team_Manager       123
1            Agent              10
2            Agent              10
5            Agent              11
6            Agent              11
7            Agent              11
8            Agent              12
456          Center_Manager     10001
15           Team_Manager       456
9            Agent              15

我希望所有代理直接或间接向经理(Team_Manager 或 Center_Manager 或 Functional_Manager)报告

我想用上面的数据生成下面的结果:-

 EMPLOYEE_NO            TEAM_ROLE            AGENT_EMP_NO
 10001                  Functional_Manager   1
 10001                  Functional_Manager   2
 10001                  Functional_Manager   5
 10001                  Functional_Manager   6
 10001                  Functional_Manager   7
 10001                  Functional_Manager   8
 10001                  Functional_Manager   9
 123                    Center_Manager       1
 123                    Center_Manager       2
 123                    Center_Manager       5
 123                    Center_Manager       6
 123                    Center_Manager       7
 123                    Center_Manager       8
 10                     Team_Manager         1
 10                     Team_Manager         2
 11                     Team_Manager         5
 11                     Team_Manager         6
 11                     Team_Manager         7
 12                     Team_Manager         8
 1                      Agent                1
 2                      Agent                2
 5                      Agent                5
 6                      Agent                6
 7                      Agent                7
 8                      Agent                8
 9                      Agent                9

我想这可以在 connect by 查询的帮助下完成,但我不太确定该怎么做。非常感谢任何帮助。

【问题讨论】:

您能否以正确的格式提供需求。或提供您的要求的快照 @JimMacaulay 很抱歉格式混乱。现在以正确的格式编辑。 【参考方案1】:

请在下方查询

with your_data as ( 
select 10001 employee_no,'Functional_Manager' team_role, null manager_emp_no from dual union 
select 123,'Center_Manager',10001 from dual union 
select 10,'Team_Manager',123 from dual union 
select 11,'Team_Manager',123 from dual union 
select 12,'Team_Manager',123 from dual union 
select 1,'Agent',10 from dual union 
select 2,'Agent',10 from dual union 
select 5,'Agent',11 from dual union  
select 6,'Agent',11 from dual union 
select 7,'Agent',11 from dual union 
select 8,'Agent',12 from dual union 
select 456,'Center_Manager',10001 from dual union 
select 15,'Team_Manager',456 from dual union 
select 9,'Agent',15 from dual), 
t1 as (SELECT  manager_emp_no,  team_role,  employee_no as agent_emp_no, sys_connect_by_path( manager_emp_no, ',' ) path 
FROM your_data 
start with manager_emp_no is null 
CONNECT BY PRIOR employee_no = manager_emp_no), 
t2 as (select 
      t1.manager_emp_no,
      t1.team_role,
      t1.agent_emp_no,
      regexp_substr(t1.path, '[^,]+', 1, commas.column_value)  as connect_by
    from
      t1,
      table(cast(multiset(select level from dual connect by  level <= length (regexp_replace(t1.path, '[^,]+'))  + 1) as sys.OdciNumberList)) commas) 
SELECT distinct connect_by_root agent_emp_no as manager_emp_no ,connect_by_root team_role as team_role,agent_emp_no
FROM t2
where agent_emp_no in (select employee_no from your_data where team_role = 'Agent')
CONNECT BY PRIOR agent_emp_no = connect_by
order by manager_emp_no desc nulls first, agent_emp_no asc

输出

"MANAGER_EMP_NO"    "TEAM_ROLE" "AGENT_EMP_NO"
10001   "Functional_Manager"    1
10001   "Functional_Manager"    2
10001   "Functional_Manager"    5
10001   "Functional_Manager"    6
10001   "Functional_Manager"    7
10001   "Functional_Manager"    8
10001   "Functional_Manager"    9
456 "Center_Manager"    9
123 "Center_Manager"    1
123 "Center_Manager"    2
123 "Center_Manager"    5
123 "Center_Manager"    6
123 "Center_Manager"    7
123 "Center_Manager"    8
15  "Team_Manager"  9
12  "Team_Manager"  8
11  "Team_Manager"  5
11  "Team_Manager"  6
11  "Team_Manager"  7
10  "Team_Manager"  1
10  "Team_Manager"  2
9   "Agent" 9
8   "Agent" 8
7   "Agent" 7
6   "Agent" 6
5   "Agent" 5
2   "Agent" 2
1   "Agent" 1

【讨论】:

谢谢!这就像一个魅力!虽然很抱歉,我现在已经对所需结果进行了一些修改......让我知道如何实现......您的查询似乎很复杂......所以只是想知道是否可以用更简单的方式实现同​​样的效果方式......不过只是一个想法! @Soumya7oct 我更新了查询以匹配您修改后的输出。我不知道任何更简单的方法来做到这一点对不起:D 请不要忘记将答案标记为正确:-)

以上是关于Oracle SQL 查询让所有代理直接或间接向经理报告的主要内容,如果未能解决你的问题,请参考以下文章

显示管理器下的所有用户(直接或间接) - SQL

在Oracle中定义SQL查询。索引为啥不能直接从select语句中引用?求教,谢谢

sql关联后没有值怎么办

SQL Oracle中子查询的工作

如何查询Oracle中所有用户信息

oracle 修改表结构或修改视图时很慢,直接让PLSQL卡死,不知道是啥原因? 重启oracle 服务却又可以了!