Oracle分层查询 - 需要扩展信息

时间:2017-10-09 15:32:21

标签: oracle hierarchical


我有以下数据代表员工层次结构,我需要识别员工/主管的所有可能的排列。 我已经看过oracle Tree Walk,看起来它应该可以完成这项工作,但我无法让它工作。问题是我需要为每个经理/员工组合列出一行,其中包括经理经理等。 (我已编辑添加名称以进行查询)

有没有人有想法如何做到这一点?

数据

previous()

期望的结果

<?php
$string = "<para><embed view='embed'>[...]</embed></para>";
$new  = strip_tags($string, '<para>'); 
echo $new;
?>

2 个答案:

答案 0 :(得分:2)

在标准分层查询中使用connect_by_root,并使用unionminus添加非管理员的员工:

select connect_by_root(super) super, emp from data connect by super = prior emp
union
select emp, null from data minus select super, null from data

测试:

with data (super, emp) as (
    select   1, 100 from dual union all
    select 100, 200 from dual union all
    select 200, 300 from dual )
select connect_by_root(super) super, emp from data connect by super = prior emp
union
select emp, null from data minus select super, null from data

结果:

     SUPER        EMP
---------- ----------
         1        100
         1        200
         1        300
       100        200
       100        300
       200        300
       300 
7 rows selected

答案 1 :(得分:0)

尝试此查询。请注意,您需要使用300, NULL的行在结果集中包含300, NULL

    WITH yourtable (super, emp )
     AS (SELECT 1,
                100
         FROM   dual
         UNION ALL
         SELECT 100,
                200
         FROM   dual
         UNION ALL
         SELECT 200,
                300
         FROM   dual
         UNION ALL
         SELECT 300,
                NULL
         FROM   dual),
     datatable
     AS (SELECT super,
                Ltrim(Sys_connect_by_path(emp, ','), ',') path
         FROM   yourtable
         START WITH emp IS NULL
         CONNECT BY PRIOR super = emp)
SELECT DISTINCT super,
                Regexp_substr(path, '[^,]+', 1, LEVEL) AS data
FROM   datatable
CONNECT BY Regexp_substr(path, '[^,]+', 1, LEVEL) IS NOT NULL
ORDER  BY super;