已提供给每个客户的融资总额
Total amount of financing already provided to each customer
我有三个独立的 table:
SystemA_Cash
SystemA_Loans
SystemB_CarLoans
每个 table 都有 Cash_AdvanceID
字段,Customer
,以及 Funded Amount
。
我需要合计每个客户的入金金额。
每个客户也可以出现在任何 table 中。
到目前为止我做到了
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;
似乎没有 return 一个 table 与客户和每个人资助的总额。
您可以为此使用 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;
以上内容适用于至少出现在 table SystemA_CashAdvances 中的客户。如果它们只出现在另一个 table 中,它们将被排除在结果之外。要将它们也放入,您需要再次使用 UNION
,这使我们回到上面的第一个解决方案。
我有三个独立的 table:
SystemA_Cash
SystemA_Loans
SystemB_CarLoans
每个 table 都有 Cash_AdvanceID
字段,Customer
,以及 Funded Amount
。
我需要合计每个客户的入金金额。
每个客户也可以出现在任何 table 中。
到目前为止我做到了
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;
似乎没有 return 一个 table 与客户和每个人资助的总额。
您可以为此使用 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;
以上内容适用于至少出现在 table SystemA_CashAdvances 中的客户。如果它们只出现在另一个 table 中,它们将被排除在结果之外。要将它们也放入,您需要再次使用 UNION
,这使我们回到上面的第一个解决方案。