我有一个包含以下列的表格:
employee_id number (PK);
unique_emp_id varchar2(20);
emp_uid varchar2(20);
我想选择所有重复的emp_uid
,其中至少一个unique_emp_id
是like '%-%'
和一个not like '%-%'
。我该怎么办?
示例数据:
emp_uid unique_emp_id
--------- -------------
12345.12 12345.12
12345.12 12345.12-1
12345.12 12345.12-2
12345.34 12345.34-1
12345.34 12345.34-2
结果数据:
emp_uid unique_emp_id
-------- -------------
12345.12 12345.12
12345.12 12345.12-1
12345.12 12345.12-2
答案 0 :(得分:1)
如果我理解正确,group by
和having
解决了此问题:
select emp_uid
from t
group by emp_uid
having sum(case when unique_emp_id like '%-%' then 1 else 0 end) > 0 and
sum(case when unique_emp_id not like '%-%' then 1 else 0 end) > 0;
答案 1 :(得分:0)
我会使用exists
:
select t.*
from table t
where exists (select 1 from table t1 where t1.emp_uid = t.emp_uid and t1.unique_emp_id like '%-%') and
exists (select 1 from table t1 where t1.emp_uid = t.emp_uid and t1.unique_emp_id not like '%-%');