Sql ORACLE-如果发现两个或多个“相同”记录,则从结果集中删除

Sql ORACLE-如果发现两个或多个“相同”记录,则从结果集中删除,sql,oracle,Sql,Oracle,假设我有这张桌子。我需要一个select语句,该语句将只查找状态为0的记录,但如果存在状态为1的同一记录,就像ID为1和2的记录一样 因此,查询必须只找到第三条和第四条记录。尝试此查询: ID | NAME | REFERENCE | STATUS ------------------------------- 1 | name1 | 123 | 0 2 | name1 | 123 | 1 3 | name2 | 111 | 0

假设我有这张桌子。我需要一个select语句,该语句将只查找状态为0的记录,但如果存在状态为1的同一记录,就像ID为1和2的记录一样

因此,查询必须只找到第三条和第四条记录。

尝试此查询:

ID | NAME  | REFERENCE | STATUS
-------------------------------
 1 | name1 | 123       | 0     
 2 | name1 | 123       | 1     
 3 | name2 | 111       | 0     
 4 | name3 | 222       | 0     
 5 | name5 | 555       | 1     

WHERE子句中对B.REFERENCE的NULL检查基本上确保在左连接中找不到匹配的记录。您也可以使用B.ID或B.NAME。

根据建议的结果,我将您的问题理解为“仅查找状态为0的记录”,其中相同名称/引用组合没有状态为1的记录

一种方法是使用not exists子句:

减号可用于获取状态为0而非1的值

select t.*
from table t
where status = 0 and
      not exists (select 1
                  from table t2
                  where t2.name = t.name and
                        t2.reference = t.reference and             
                        t2.status = 1
                 );      
select t.*
from table t
where status = 0 and
      not exists (select 1
                  from table t2
                  where t2.name = t.name and
                        t2.reference = t.reference and             
                        t2.status = 1
                 );      
SELECT ID, Name, Reference, Status 
FROM   Table1
WHERE  (Name, Reference) IN (SELECT Name, Reference
                             FROM   Table1
                             WHERE  Status = 0
                             MINUS
                             SELECT Name, Reference
                             FROM   Table1
                             WHERE  Status = 1)
        SELECT * FROM TABLE WHERE NAME NOT IN
          (
           SELECT NAME FROM 
           TABLE 
           GROUP BY NAME
           HAVING COUNT(DISTINCT STATUS)>1
          ) AND STATUS='0';