联盟表上的运行余额

时间:2017-08-30 09:35:31

标签: sql sql-server union running-total running-balance

以下代码计算表格PAYMENTselect b.payment_date, a.account_no, a.accountname, a.loan_amount, b.amount, ob = a.loan_amount - sum(b.amount) over (partition by b.account_no order by b.payment_date) from account a inner join (select * from payment) b on a.account_no = b.account_no order by a.account_no, b.payment_date 的未结余额:

    +--------------+------------+----------+
    | payment_date | account_no |  amount  |
    +--------------+------------+----------+
    | 2017-08-10   |  123456789 |   5000   |
    | 2017-08-15   |  987654321 |   3000   |
    | 2017-09-15   |  987654321 |   3000   |
    | 2017-10-11   |  123456789 |   4000   |
    | 2017-10-16   |  987654321 |   3500   |
    | 2017-11-10   |  123456789 |   3000   |
    | 2017-11-15   |  987654321 |   2500   |
    +--------------+------------+----------+

付款

    +--------------+-------------+---------------+
    | account_no   | accountname |  loan_amount  |
    +--------------+-------------+---------------+
    |  123456789   |     John    |   15000       |
    |  987654321   |     Jane    |   20000       |
    +--------------+-------------+---------------+

帐户

    +--------------+------------+----------+----------------------+
    | payment_date | account_no |  amount  | outstanding_balance  |
    +--------------+------------+----------+----------------------+
    | 2017-08-10   |  123456789 |   5000   |        10000         |
    | 2017-10-11   |  123456789 |   4000   |         6000         |
    | 2017-11-10   |  123456789 |   3000   |         3000         |
    | 2017-08-15   |  987654321 |   3000   |        17000         |
    | 2017-09-15   |  987654321 |   3000   |        14000         |
    | 2017-10-16   |  987654321 |   3500   |        11500         |
    | 2017-11-15   |  987654321 |   2500   |         9000         |
    +--------------+------------+----------+----------------------+

QUERY RESULT

    +--------------+------------+----------+
    | payment_date | account_no |  amount  |
    +--------------+------------+----------+
    | 2017-08-10   |  123456789 |   100    |
    | 2017-08-15   |  987654321 |   100    |
    | 2017-09-15   |  987654321 |   100    |
    +--------------+------------+----------+

我想在计算中添加折扣表。此表包含与付款表格相同的结构。

折扣

select 
    b.payment_date, 
    a.account_no, a.accountname, a.loan_amount,
    b.amount,
    ob = a.loan_amount - sum(b.amount) over (partition by b.account_no order by b.payment_date) 
from 
    account a 
inner join
    (select * from payment union select * from discount) b on a.account_no = b.account_no 
order by 
    a.account_no, b.payment_date

所以我决定在下面的查询中添加一个UNION。但它返回不正确。值。请帮忙

.fixed-header #header {
  background-color: rgba(1,1,1,0.0) !important;
}
.fixed-header #header {
  background-color: transparent;
}

2 个答案:

答案 0 :(得分:0)

使用另一个INNER JOIN

select 
    b.payment_date, 
    a.account_no, 
    a.loan_amount,
    b.amount,
    ob = a.loan_amount - sum(b.amount) over (partition by b.account_no order by b.payment_date),
    d.amount 
from 
    account a 
inner join
    payment b on a.account_no = b.account_no 
inner join
    discount d on (d.account_no = b.account_no and d.payment_date = b.payment_date)
order by 
    a.account_no, b.payment_date

答案 1 :(得分:0)

折扣金额未反映在您的计算中。 ob = a.loan_amount - sum(b.amount) 您可能不一定需要联合,因为折扣是一个单独的表,您可以只是内部加入表并使用折扣金额来减少loan_amount ob =(a.loan_amount- discount_amount) - sum(b.amount)