【问题标题】:How to get distinct employees that do not have a particular skillset如何获得不具备特定技能的独特员工
【发布时间】:2019-08-13 19:52:39
【问题描述】:

我有一个有两列的表。 Employee_id(每个员工唯一)和员工技能集的下一列。一名员工可以拥有多种技能。如果 A、B、C、D、E 是员工可以拥有的五种技能组,我如何检索没有技能组“c”的不同员工的列表。

employee_id skillset
1           A
1           C
2           E
3           A
3           B
3           C
4           D
4           C
5           B

我已经尝试了自我加入和其他方法,但它不起作用。

select distinct employee_id from employee_skillset where skillset not like 'C'

当我运行查询时,它仍然给我具有“c”技能集的employee_ids

【问题讨论】:

  • 好吧,您考虑过为什么会得到这些答案吗?基本上,您的查询首先选择技能集不是 C 的所有行。然后您要求提供不同的 ID。听起来好像你需要来自另一端的攻击。
  • 使用带有NOT EXISTS的子查询。

标签: sql oracle oracle10g


【解决方案1】:

您可以group by employee_id并在HAVING子句中设置条件:

select employee_id 
from employee_skillset 
group by employee_id
having sum(case when skillset = 'C' then 1 else 0 end) = 0

或者不存在:

select distinct s.employee_id 
from employee_skillset s
where not exists (
  select 1 from employee_skillset
  where employee_id = s.employee_id and skillset = 'C'
)  

【讨论】:

    【解决方案2】:

    您的数据集的预期结果是什么? 2和5?

    为什么不像下面这样

    SELECT DISTINCT employee_id
    FROM Table1
    WHERE skillset <> 'C';
    

    【讨论】:

    • 为什么不如下所示,因为如果员工还拥有其他技能,这将返回具有技能“C”的员工。
    【解决方案3】:

    MINUS 设置运算符是一种选择:

    SQL> with employee_skillset (employee_id, skillset) as
      2    (select 1, 'a' from dual union all
      3     select 1, 'c' from dual union all
      4     select 2, 'e' from dual union all
      5     select 3, 'a' from dual union all
      6     select 3, 'b' from dual union all
      7     select 3, 'c' from dual union all
      8     select 4, 'd' from dual union all
      9     select 4, 'c' from dual union all
     10     select 5, 'b' from dual
     11    )
     12  select employee_id from employee_skillset
     13  minus
     14  select employee_id from employee_skillset where skillset = 'c';
    
    EMPLOYEE_ID
    -----------
              2
              5
    
    SQL>
    

    另一种选择:

     <snip>
     12  select employee_id
     13  from (select employee_id,
     14               case when skillset = 'c' then 1 else 0 end flag
     15        from employee_skillset
     16       )
     17  group by employee_id
     18  having sum(flag) = 0;
    
    EMPLOYEE_ID
    -----------
              2
              5
    
    SQL>
    

    或者:

     <snip>
     12  select employee_id
     13  from (select employee_id,
     14               listagg(skillset, ',') within group (order by null) lagg
     15        from employee_skillset
     16        group by employee_id
     17       )
     18  where instr(lagg, 'c') = 0;
    
    EMPLOYEE_ID
    -----------
              2
              5
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 2020-09-19
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      相关资源
      最近更新 更多