在我的查询中,我有 4 个表(A、B、C 和 D),并使用下面提供的条件加入它们。我需要返回仅存在于表 D 中的记录(objectid)。表 D 中的 objectid 都是唯一的。
表 D 只有 100 万条记录,但是当我运行下面的查询时,它给了我 500 万条记录。请帮助我解决问题。
select D.*
from Table_A A
join Table_B B on B.documentid = a.documentid
join Table_C C on B.objectid = C.formid and
B.instance_seq = C.instance_seq
and
C.src_exp = date '9999-12-31'
join Table_D D on C.sourceid = D.objectid
and
C.instance_seq = D.instance_seq
and
D.src_exp = date '9999-12-31'
where A.src_exp = date '9999-12-31' and A.form = 'Tentative'
仅从
D
中选择,然后检查其他表中是否有 EXISTS
的匹配数据:
SELECT *
FROM Table_D D
WHERE EXISTS(
SELECT 1
FROM Table_A A
INNER JOIN Table_B B
ON B.documentid = a.documentid
INNER JOIN Table_C C
ON B.objectid = C.formid
AND B.instance_seq = C.instance_seq
WHERE C.src_exp = date '9999-12-31'
AND C.sourceid = D.objectid
AND C.instance_seq = D.instance_seq
AND A.src_exp = date '9999-12-31'
AND A.form = 'Tentative'
)
AND D.src_exp = date '9999-12-31'