【问题标题】:Employee AND Manager员工和经理
【发布时间】:2019-11-01 15:54:16
【问题描述】:

我有数据:EMPID, NAME, MANAGER_ID

我要回:EMPID, NAME, COLLEAGUE_ID

COLLEAGUE_ID 将是与EMP_ID 中的人具有相同经理的人员列表

任何帮助都会很棒。即使它只是我需要完成的功能类型的名称。

【问题讨论】:

  • 你有没有尝试过?
  • 我建议您阅读任何有关 SQL JOIN 的教程,这应该在那里更全面地介绍
  • 提示:不使用图片的原因是here

标签: sql sql-server tsql


【解决方案1】:

这听起来基本上像cross join。但是因为你的表中不包括经理,所以你还需要union all

select t1.emp_id, t1.name, t2.emp_id as colleague
from t t1 join
     t t2
     on t1.manager = t2.manager and t1.emp_id <> t2.emp_id
union all
select t1.emp_id, t1.name, t1.manager
from t;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2020-03-12
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多