我有两张桌子
MS Access
中的
贷款包括
Loan_Payments包括
贷款表
Ref_Loan Loan_Amount
loan 1 12000
loan 2 24000
loan 3 5000
Loan_Payments
Ref_Loan Amount_Paid Date
loan 1 1000 01/01/2011
loan 1 1000 01/02/2011
loan 1 1000 01/03/2011
loan 2 1000 01/01/2011
loan 2 1000 01/02/2011
我想要一个像这样展示的外线;
Ref_loan Amount_Paid Date Balance
loan 1 1000 01/01/2011 11000
loan 1 1000 01/02/2011 10000
loan 1 1000 01/03/2011 9000
loan 2 1000 01/01/2011 23000
loan 2 1000 01/02/2011 22000
loan 3 5000
答案 0 :(得分:0)
访问查询:
select L.Ref_Loan, P.Amount_Paid, P.Date,
L.Loan_Amount - DSum("Amount_Paid", "Loan_Payment",
"Ref_Loan=" & L.Ref_Loan & " AND Date<= #" & Format(P.Date, "yyyy/mm/dd") & "#")
from Loan AS L
left join Loan_Payments AS P on P.Ref_Loan = L.Ref_Loan
ORDER BY L.Ref_Loan, P.Date
但是,如果您使用的是来自C#的Jet,则DSum可能无法正常工作。在这种情况下,请完整地写出正确的SQL
select L.Ref_Loan, P.Amount_Paid, P.Date,
L.Loan_Amount - (
SELECT SUM(P2.amount_paid)
FROM Loan_Payment AS P2
WHERE P2.Ref_Loan=L.Ref_Loan
AND P2.Date <= P.Date)
from Loan AS L
left join Loan_Payments AS P on P.Ref_Loan = L.Ref_Loan
ORDER BY L.Ref_Loan, P.Date