我有三个单独的表:
SystemA_Cash
SystemA_Loans
SystemB_CarLoans
每个表格都有Cash_AdvanceID
字段,Customer
和& Funded Amount
。
我需要为每位客户提供总额。
每个客户也可以出现在任何表格中。
到目前为止我做了
SELECT SystemA_CashAdvances.CashAdvanceID, SystemA_CashAdvances.Customer,
SystemA_CashAdvances.Funded_Amount
FROM
SystemA_CashAdvances
INNER JOIN
SystemA_Loans ON SystemA_Loans.CashAdvanceID = SystemA_CashAdvances.Cash_AdvanceID
INNER JOIN
SytemB_CarLoans ON SystemA_CashAdvances.Cash_AdvanceID = SystemB_CarLoans.CashAdvanceID;
它似乎没有与客户一起返回一张表,而且每笔表的总金额都已获得资助。
答案 0 :(得分:0)
您可以使用UNION
和GROUP BY
,例如:
SELECT Cash_AdvanceID,
Customer,
SUM(Funded_Amount) AS "Total_Funded_Amount"
FROM (
SELECT Cash_AdvanceID,
Customer,
Funded_Amount
FROM SystemA_CashAdvances
UNION
SELECT Cash_AdvanceID,
Customer,
Funded_Amount
FROM SystemA_Loans
UNION
SELECT Cash_AdvanceID,
Customer,
Funded_Amount
FROM SytemB_CarLoans
)
GROUP BY Cash_AdvanceID,
Customer;
使用JOIN
执行此操作会很困难,因为MS Access不像其他数据库引擎那样支持完全外部联接。最接近的是单侧外连接,如下所示:
SELECT a.Cash_AdvanceID,
a.Customer,
a.Funded_Amount + Nz(l.Funded_Amount)
+ Nz(c.Funded_Amount) AS "Total_Funded_Amount"
FROM (SystemA_CashAdvances AS a
LEFT JOIN SystemA_Loans AS l
ON a.Cash_AdvanceID = l.Cash_AdvanceID)
LEFT JOIN SytemB_CarLoans AS c
ON a.Cash_AdvanceID = c.Cash_AdvanceID;
以上内容适用于至少出现在 SystemA_CashAdvances 表中的客户。如果它们只出现在另一个表中,它们将被排除在结果之外。为了获得这些内容,您需要再次使用UNION
,这会让我们回到上面的第一个解决方案。