我有一个通过employeeid = managerid
取消循环的员工表我希望在结果集中拥有{employeeid,1-level-down-from-request}
所以给出
Employeeid - Managerid
Bob - Null
Jill - Bob
John - Bob
Suzy - Jill
Shannon - Jill
Shawn - Jill
Ron - John
Rick - John
Xavier - Shawn
如果我在Bob上运行查询,我们会得到
Shannon - Jill
Shawn - Jill
Ron - John
Rick - John
Xavier - Jill
注意Xavier去了Jill,而不是Shawn,因为Jill是Xavier的高级经理(比Bob低一步)
答案 0 :(得分:3)
最简单的方法可能是进行自我加入
SELECT employee.employeeId,
employee.managerId
FROM table_name mgr,
table_name subordinate,
table_name employee
WHERE mgr.managerId = subordinate.employeeId
AND subordinate.managerId = employee.managerId
AND mgr.employeeId = 'Bob'
然而,构建分层查询会更有效,更通用。像
这样的东西SELECT *
FROM (SELECT employeeId, managerId, level lvl
FROM table_name
START WITH employeeId = 'Bob'
CONNECT BY prior employeeId = managerId)
WHERE lvl = 3
显然,更改后一个查询以更改是否需要直接报告或二级报告或某个其他级别的层次结构会更容易。而且它通常也会更有效率。