简体   繁体   English

Oracle 12c-选择具有序列的记录

[英]Oracle 12c - select records that have sequences

I have a table with the following columns: 我有一个包含以下列的表格:

employee_id number (PK);
unique_emp_id varchar2(20);
emp_uid varchar2(20);

I want to select all duplicate emp_uid 's where at least one unique_emp_id is like '%-%' and one not like '%-%' . 我想选择所有重复的emp_uid ,其中至少一个unique_emp_id like '%-%'not like '%-%' How can I do this? 我怎样才能做到这一点?

Example data: 示例数据:

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

Result data: 结果数据:

emp_uid   unique_emp_id
--------  -------------
12345.12  12345.12
12345.12  12345.12-1
12345.12  12345.12-2

If I understand correctly, group by and having solve this problem: 如果我理解正确的话, group byhaving解决这个问题:

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;

I would use exists : 我会用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 '%-%');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM