【问题标题】:Oracle 12c - select records that have sequencesOracle 12c - 选择具有序列的记录
【发布时间】:2019-03-26 19:12:33
【问题描述】:

我有一个包含以下列的表格:

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

【问题讨论】:

  • 请提供样本数据和期望的结果。

标签: sql oracle select oracle12c


【解决方案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;

【讨论】:

    【解决方案2】:

    我会使用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 '%-%');
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-13
      • 2013-08-17
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      相关资源
      最近更新 更多