从提到员工和主管的 table 中获取从属值列表

Get the subordinate list of values from a table where the employee and supervisor is mentioned

我有如下数据集

employee Supervisor
A B
B C
D E
F B
G F

在此数据集中,当我将主管作为输入时,我想逐一访问每个员工。 例如,如果我将 B 作为输入,首先得到 A,然后像那样去 B 直到结束,然后回到 F,然后去 G。 我想让它们按顺序排列,这样我就必须将最高主管的一些数据应用于整个数据集。 提前谢谢你。

我是这样理解问题的;第一列 (employee) 显示所有隶属于主管的员工(在本例中为 c),而 path 显示......好吧,路径 从选定的主管到该员工。

SQL> with dataset (employee, supervisor) as
  2    (select 'a', 'b' from dual union all
  3     select 'b', 'c' from dual union all
  4     select 'd', 'e' from dual union all
  5     select 'f', 'b' from dual union all
  6     select 'g', 'f' from dual
  7    )
  8  select
  9    employee,
 10    supervisor,
 11    ltrim(sys_connect_by_path(employee, ' - '), ' - ') as path
 12  from dataset
 13  start with supervisor = 'c'                 --> this is the supervisor you're interested in
 14  connect by supervisor = prior employee;

E S PATH
- - --------------------
b c b
a b b - a
f b b - f
g f b - f - g

SQL>

如果你想“循环”员工,那么你将需要 PL/SQL 和......好吧,循环。像这样:

SQL> set serveroutput on
SQL> declare
  2    l_supervisor varchar2(1) := 'c';
  3  begin
  4    for cur_r in
  5      (with dataset (employee, supervisor) as
  6          (select 'a', 'b' from dual union all
  7           select 'b', 'c' from dual union all
  8           select 'd', 'e' from dual union all
  9           select 'f', 'b' from dual union all
 10           select 'g', 'f' from dual
 11          )
 12        select
 13          employee,
 14          supervisor,
 15          ltrim(sys_connect_by_path(employee, ' - '), ' - ') as path
 16        from dataset
 17        start with supervisor = l_supervisor
 18        connect by supervisor = prior employee
 19      )
 20    loop
 21      -- you'd do something with this employee; I'm just displaying it
 22      dbms_output.put_line('Employee = ' || cur_r.employee);
 23    end loop;
 24  end;
 25  /
Employee = b
Employee = a
Employee = f
Employee = g

PL/SQL procedure successfully completed.

SQL>