2017-08-30 55 views
0

下面的代碼計算爲表ACCOUNTPAYMENT運行餘額:在聯盟表運行平衡

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) 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   | 
    +--------------+------------+----------+----------------------+ 

我想補充折扣表中計算。此表格包含與付款表格相同的結構。

折扣

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

所以我決定在我下面的查詢添加一個UNION。但它返回不正確。值。請幫助

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 
+0

您可以添加Rextester表格定義和一些測試數據嗎?將更容易爲我們重現和測試 –

回答

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 
0

折扣金額未在計算反映。 ob = a.loan_amount - 總額(b.amount) 您可能不一定需要聯合,因爲折扣是一個單獨的表,您可以將內部聯接表並使用折扣金額來減少loan_amount ob = (a.loan_amount - discount_amount) - 總和(b.amount)