请指导我用sql查询计算余额列
表信息: 表: - 交易,账户
交易表字段:ref_no,accounting_dt,debit_credit,amt,currency,account_no 帐户表字段:account_no,account_name,bal_on_a_day,货币
表格中的数据
交易表
ref_no accounting_dt debit/credit amt currency Balance
ab523 02/02/2018 D 10 840 10555589
bc654 03/02/2018 C 20 840 10555589
ab588 05/02/2018 D 20 840 10555589
bc655 05/02/2018 D 60 840 10555589
ab589 06/05/2018 C 80 840 10555589
bc656 06/05/2018 C 80 840 10555589
帐户表
account_no account_name bal_on_a_day currency
10555589 Vishal 500 840
10558889 Amit 520 840
55558889 Rohit 880 840
70561089 Anup 990 840
93062739 Sneha 440 840
15564389 Smita 560 840
结果必须如下:
ref_no accounting_dt debit_amt credit_amt Balance
bal_on_a_day 500
ab523 02/02/2018 10 490 (500-10)
bc654 03/02/2018 20 510 (490+20)
ab588 05/02/2018 20 490 (510-20)
bc655 05/02/2018 60 430 (490-60)
ab589 06/05/2018 80 510 (430+80)
bc656 06/05/2018 80 590 (510+80)
Total dr,cr,clos bal 90 180 590