【问题标题】:How to compare different values within the same column如何比较同一列中的不同值
【发布时间】:2023-01-08 13:28:19
【问题描述】:

我有两个表 emp 和类型。

create table EMP(ID number(10), effective_date date);
EID    Effective_date
--------------------
1     02/14/2023
2     02/15/2023
3     04/30/2023
4     03/24/2023

create table type(ID number(10),contract_type varchar2(2));
TID  contract_type
------------------
1       P
1       S
1       P
2       S
2       S
3       P
3       S
4       S

我正在查看合同类型在类型表中为“S”的 EID。 (或者生效日期的emp表大于sysdate并且类型表中只有contract_type ='S')

实际结果 :
2个
4个

我的查询没有给出正确的结果。

select emp.EID
 from emp,type
 where EID = TID
         contract_type ='S'
         effective_date >= sysdate
         group by TID 
         having count(TID) >= 1;  

【问题讨论】:

  • 请在您的问题中将预期结果添加为表格。

标签: sql oracle plsql


【解决方案1】:

我会在这里使用存在的逻辑:

SELECT EID
FROM EMP e
WHERE effective_date >= SYSDATE AND
      NOT EXISTS (
          SELECT 1
          FROM "type" t
          WHERE t.TID = e.EID AND
                t.contract_type <> 'S'
      );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多