如果不存在或不存在

时间:2017-05-11 20:09:01

标签: sql sql-server sql-server-2014

所有这些表ERTBatchesERTBatchChecksERTClaims都有一个标识符 可以说标识符是1010.
该标识符必须存在于所有三个表中
我试图找出是否所有三个表中都没有任何标识符
这些是标识符的名称:

ERTBatches."BatchIdentifier"
ERTClaims."ERTBatchNumber"
ERTBatchChecks."BatchIdentifier"

1 个答案:

答案 0 :(得分:5)

一种方法是union all和聚合:

select id
from ((select b.BatchIdentifier as id, 1 as which from ERTBatches b) union all
      (select bc.BatchIdentifier as id, 2 as which from ERTBatchChecks bc) union all
      (select c.ERTBatchNumber as id, 3 as which from ERTClaims c)
     ) t
group by id
having count(distinct which) < 3;