0

我如何选择以显示有贷款但没有 dda 且没有相关 actype 的 custid,除非只有 actype L?寻找没有 dda 关系的记录,即使它们是联合相关的 (cfrela = J)。如下所示,A123 和 B128 符合条件,但相关的 A123 D actype 由于与 DDA 账户的共同关系而被取消资格。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
4

2 回答 2

1

如果我正确理解您的问题,您想显示 dda 表中不存在且仅存在于 actype = L 的相关表中的客户 ID?

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

如果必须在相关表中有记录,则再添加一个 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

希望这可以帮助。

于 2013-02-11T22:14:33.133 回答
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子句根据您的逻辑选择不匹配的记录。

于 2013-02-11T22:14:01.180 回答