select ca.cust_ac_no, ca.ccy, ah.trn_dt,
(SELECT sum(COALESCE(hi.lcy_amount,0))
FROM actb_history hi
WHERE hi.ac_no='0013001600038' and hi.drcr_ind = 'C' and ah.trn_dt = hi.trn_dt
GROUP BY hi.ac_no,hi.drcr_ind) as Total_Credits,
(SELECT sum(COALESCE(hi.lcy_amount,0))
FROM actb_history hi
WHERE hi.ac_no='0013001600038' and hi.drcr_ind = 'D'and ah.trn_dt = hi.trn_dt
GROUP BY hi.ac_no,hi.drcr_ind) as Total_Debits,
((SELECT sum( COALESCE(hi.lcy_amount,0))
FROM actb_history hi
WHERE hi.ac_no='0013001600038' and hi.drcr_ind = 'C' and ah.trn_dt = hi.trn_dt
GROUP BY hi.ac_no,hi.drcr_ind)
-
(SELECT sum(COALESCE(hi.lcy_amount,0))
FROM actb_history hi
WHERE hi.ac_no='0013001600038' and hi.drcr_ind = 'D'and ah.trn_dt = hi.trn_dt
GROUP BY hi.drcr_ind,hi.drcr_ind )) as difference
from actb_history ah, sttm_cust_account ca
where ah.ac_no='0013001600038'
and ah.ac_no = ca.cust_ac_no
group by ca.cust_ac_no, ca.ccy, ah.trn_dt
上面的代码仅在 total_credits 和 total_debits 有对应值的情况下计算差异
公式是贷方-借方=差额。问题是一些交易只有一个,而另一个是空的。所以添加空值后结果为空。
我的愿望是它应该为任何空值显示零并能够执行减法。
请帮忙。