SQL:提取不同的值

时间:2016-03-07 16:03:59

标签: sql oracle-sqldeveloper

我有一张桌子上有两个col

EmpID  Beneifts
1       A
1       B
2       A
3       A
3       c
4       A

我的预期输出就是只有效益A

EmpID   Benefits
2        A
4        A

请帮我查询。

1 个答案:

答案 0 :(得分:0)

使用NOT EXISTS返回一行相同的EmpID除了'A'没有任何好处:

select *
from tablename t1
where t1.Benefits = 'A'
  and not exists (select 1 from tablename t2
                  where t2.EmpID = t1.EmpID
                    and t2.Benefits <> 'A')

或者,执行GROUP BY

select EmpID
from tablename
group by EmpID
having min(Benefits) = 'A'
   and max(Benefits) = 'A'