【问题标题】:. Create an “Employee Roster” report listing the Manager’s name and the name of the employees who report to that manager.创建一个“员工名册”报告,列出经理的姓名和向该经理报告的员工的姓名
【发布时间】:2020-02-24 20:01:27
【问题描述】:

我认为我应该在这里使用联接,但是,我不太确定报告是如何在 SQL 中出现的,因为他们报告的员工和经理在同一个表中enter image description here

【问题讨论】:

  • 欢迎来到 SO。请阅读how to ask。请贴出你的sql代码。

标签: mysql sql subquery


【解决方案1】:

在 Oracle 中,这将通过“CONNECT BY PRIOR”函数提供的递归函数得到最优雅的处理。正如您已经表明您在 Oracle 之外进行操作,您必须为您的表设置两次别名。此外,您还没有说明经理是否可能没有员工或员工可能没有经理——在这两种情况下,您都需要考虑“外部联接”条件。以下是简单案例的代码:每个员工都有一个经理,每个经理都有员工:

Select m.lastname manager_last,
m.firstname  manager_first,
e.lastname employee_last, e.firstname employee_first
from employee_list e, employee_list m
where e.reports_to = m.employee_number
order by m.lastname, m.firstname, e.lastname, e.firstname  

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2021-09-08
    • 1970-01-01
    • 2019-02-13
    • 2021-07-22
    • 2022-12-12
    • 1970-01-01
    • 2019-04-23
    • 2022-11-18
    相关资源
    最近更新 更多