如何在 MS Access 查询中获得 运行 扣除金额和 运行 余额?

How can I get Running Deduction Amount and Running Balance on MS Access Query?

我处理这个查询已经有一段时间了 (MS ACCESS 2016)...

我有两个相关表:贷款 Table 和扣除额 Table

贷款 TABLE:

loan_id   employee_id   loan_date   loan_amount  is_posted
-------   -----------   ----------  -----------  ---------
   1           1        06/01/2019   15,000.00     True
   2           4        06/01/2019    2,000.00     True

推导TABLE:

deduction_id  loan_id  deduction_date  deduction_amount  is_posted
------------  -------  --------------  ----------------  ---------
   D1_1          1       01/15/2020         500.00         True
   D1_2          1       01/30/2020         500.00         True
   D1_3          1       02/15/2020         300.00         False
   D1_4          1       02/28/2020         100.00         True
   D2_1          2       01/15/2020       1,000.00         False
   D2_2          2       01/30/2020         200.00         True
   D2_3          2       02/15/2020         500.00         True

从这些表中,我试图通过查询获得 运行 DEDUCTION 和 运行 BALANCE 以获得这种结果: (我将使用 ADODB 将此结果填充到 Excel Userform ListBox 中)

运行 DEDUCTION/BALANCE 查询:[这是期望的结果]

deduction_id  loan_id  deduction_date  deduction_amount  RunDeduct    RunBal
------------  -------  --------------  ----------------  ---------  ---------
    D1_4         1       02/28/2020         100.00       1,100.00   14,400.00
    D1_2         1       01/30/2020         500.00       1,000.00   14,500.00
    D1_1         1       01/15/2020         500.00         500.00   15,000.00
    D2_3         2       02/15/2020         500.00         700.00    1,300.00
    D2_2         2       01/30/2020         200.00         200.00    1,800.00

在此示例查询中:


我要实现的查询计算:


到目前为止,我能够使用此

获得 运行 总数
SELECT
    TD.deduction_id,
    TD.loan_id,
    TD.deduction_date,
    TD.deduction_amount,
    (SELECT Sum(deduction_amount) FROM t_deduction WHERE TD.deduction_date >= deduction_date AND TD.loan_id = loan_id) AS RunnPaid,
    TL.loan_amount-RunnPaid AS RunnBalance
FROM
    t_loan AS TL
INNER JOIN
    t_deduction AS TD ON TL.loan_id = TD.loan_id
ORDER BY
    TD.loan_id, TD.deduction_date DESC;

但是每当我尝试关联 't_deduction.is_posted' 字段时,它就会弄乱整个查询。它仍然包括计算中的 'not posted' 条记录。

SELECT
    TD.deduction_id,
    TD.loan_id,
    TL.loan_amount,
    TD.deduction_date,
    TD.deduction_amount,
    (SELECT Sum(deduction_amount) FROM t_deduction WHERE TD.deduction_date >= deduction_date AND TD.loan_id = loan_id AND TD.is_posted=True) AS RunnPaid,
    TL.loan_amount-RunnPaid AS RunnBalance
FROM
    t_loan AS TL
INNER JOIN
    t_deduction AS TD ON TL.loan_id = TD.loan_id
WHERE
    TD.is_posted = True
ORDER BY
    TD.loan_id, TD.deduction_date DESC;

提前谢谢你。

更改嵌套的 SQL 条件以使用 t_deduction.is_posted 而不是 TD.is_posted - 只需删除 TD.:

(SELECT Sum(deduction_amount) FROM t_deduction WHERE TD.deduction_date >= deduction_date 
 AND TD.loan_id = loan_id AND is_posted=True) AS RunnPaid,

我认为问题出在相关子查询中“td.is_posted”的标识符范围。您可以尝试以下代码,如果有效请告诉我:

SELECT
    TD.deduction_id, TD.loan_id, TL.loan_amount,
    TD.deduction_date, TD.deduction_amount,
    (SELECT Sum(deduction_amount) FROM t_deduction WHERE TD.deduction_date >= deduction_date AND TD.loan_id = loan_id AND is_posted) AS RunnPaid,
    TL.loan_amount-RunnPaid AS RunnBalance
FROM
    t_loan AS TL
INNER JOIN
   (SELECT deduction_id, loan_id, deduction_date, deduction_amount
    FROM t_deduction
    WHERE is_posted ) AS TD
ON TL.loan_id = TD.loan_id
ORDER BY
    TD.loan_id, TD.deduction_date DESC;

我假设“is_posted”是一个 Bool(或 Yes/No)。否则,将“is_posted”替换为“is_posted = TRUE”。