如何通过不选择子值,使所有父值从结果集中消失

how by not choosing child value, make all parent value disappear from the result set

我的数据是这样的:相同的 PolicyNumber 可以有不同的 ClassCode。那么,如果至少有一个 ClassCode 选择为 NOT IN

,如何消除所有包含 PolicyNumber PACA1000101-00 的行

所以如果我说 WHERE ClassCode <> 01183 我希望所有这些记录都不会出现在结果集中。

使用不存在且相关的子查询?

Select * 
from table T1
where not exists (Select 1 
                  from table T2 
                  where T1.PolicyNumber = T2.PolicyNumber
                    and classCode = 01183)

另一种方法,如果您必须加入具有不同值的第二个 table,并且您需要来自 table 的值...

SELECT T1.*, T2.*
FROM  table T1
LEFT JOIN (Select * 
           from table T2 
           classCode = 01183)
  on T1.PolicyNumber = T2.PolicyNumber
WHERE T2.PolicyNumber is null

这表示将 table 加入自身,但仅包括右侧 class 代码为 01183 的记录。然后 where 子句表示排除所有至少具有class 代码 01183.

出现 1 次