我如何选择显示有贷款但没有dda且没有相关行为类型的custid,除非只有actype L?寻找没有dda关系的记录,即使它们是联合相关的(cfrela = J)。 如下所述,A123和B128符合条件,但由于与DDA账户的联合关系,相关的A123 D actype被取消资格。 L =贷款,D = DDA,S =储蓄
SELECT loans.custid, dda.custid, related.custid
FROM data.lnmast lnmast
EXCEPTION JOIN data.dda dda
ON loans.custid = dda.custid
JOIN data.related related
ON loans.custid = related.custid
LOANS
custid(c) actype(c)
A123 L
B128 L
DDA
custid(c) actype(c)
A125 D
RELATED
custid(c) actype(c) cfrela(c)
A123 D J
A123 L P
A123 S J
B128 L P
B128 L P
答案 0 :(得分:1)
如果我正确理解您的问题,您希望显示dda表中不存在的客户ID,并且仅存在于actype = L的相关表中?
SELECT L.CustId
FROM Loans L
LEFT JOIN Related R ON L.custid = R.custId AND R.actype <> 'L'
LEFT JOIN DDA D ON L.custid = D.custid
WHERE d.custid IS NULL AND R.custid is NULL
如果必须在Related表中有记录,那么再添加一个INNER JOIN:
SELECT DISTINCT L.CustId
FROM Loans L
INNER JOIN Related R1 ON L.custid = R1.custId AND R1.actype = 'L'
LEFT JOIN Related R ON L.custid = R.custId AND R.actype <> 'L'
LEFT JOIN DDA D ON L.custid = D.custid
WHERE d.custid IS NULL AND R.custid is NULL
希望这有帮助。
答案 1 :(得分:0)
解决此问题的一种方法是使用left outer join
:
select l.*
from data.lnmast l left outer join
(select *
from data.dda
where actype <> 'L'
) dda
on l.custid = dda.custid left outer join
data.related r
on l.custid = dda.custid
where dda.custid is null and r.custid is null
where
子句根据您的逻辑选择不匹配的记录。