简体   繁体   中英

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 '%-%' . 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:

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 :

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 '%-%');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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