晚上好,我需要帮助尝试从我的一个分层表(用红色圈出)中检索中产阶级经理,他们不是根节点也不是叶子节点。
分层图/数据
我能够使用查询
检索所有叶节点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”,但不断得到不同的结果。
答案 0 :(得分: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)
答案 1 :(得分: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