2

晚上好,我需要帮助尝试从我的一个分层表(红色圆圈)中检索中产阶级经理,这些表既不是根节点也不是叶节点。

分层图表/数据 层次图 数据

我能够使用我的查询检索所有叶节点

select level,  first_name ||' '|| last_name "FullName" 
from more_employees
where employee_id not in (select NVL(manager_id, 0) from more_employees)
start with employee_id = 1
CONNECT by prior employee_id = manager_id;

结果

我确定我应该在 where 子句中使用关键字“in”,但会不断得到不同的结果。

4

2 回答 2

2

我相信这样的事情应该有效

select * from more_employees m1 where manager_id is not null
and exists ( select 1 from more_employees m2 where m2.manager_id=m1.employee_id)
于 2013-10-12T02:39:24.187 回答
2

您可以CONNECT_BY_IS_LEAF为此使用伪列。

select level,  first_name ||' '|| last_name "FullName" 
from more_employees
where connect_by_isleaf = 0 and manager_id is not null
start with employee_id = 1
connect by prior employee_id = manager_id;

您还可以使用它来获取所有叶子:

select level,  first_name ||' '|| last_name "FullName" 
from more_employees
where connect_by_isleaf = 1
start with employee_id = 1
connect by prior employee_id = manager_id;

这可能比您使用子选择的解决方案更快

这是一个 SQLFiddle 示例:http ://sqlfiddle.com/#!4/511d9/2

于 2013-10-12T12:03:12.180 回答