与えられたシナリオ:
table fd
(cust_id, fd_id) primary-key and amount
table loan
(cust_id, l_id) primary-key and amount
すべてのローンの合計より少ない金額の固定預金を持っているすべての顧客をリストしたいと思います。
クエリ:
SELECT cust_id
FROM fd
WHERE amount
<
(SELECT sum(amount)
FROM loan
WHERE fd.cust_id = loan.cust_id);
OR should we use
SELECT cust_id
FROM fd
WHERE amount
<
(SELECT sum(amount)
FROM loan
WHERE fd.cust_id = loan.cust_id group by cust_id);
顧客は複数のローンを持つことができますが、一度に1つのFDが考慮されます。