sql - finding the difference of the sum of a column values based on an indicator(a/b) column in a table -


select ca.cust_ac_no, ca.ccy, ah.trn_dt, (select  sum(coalesce(hi.lcy_amount,0))                                          actb_history hi       hi.ac_no='0013001600038'  , hi.drcr_ind = 'c' , ah.trn_dt = hi.trn_dt      group hi.ac_no,hi.drcr_ind) total_credits, (select  sum(coalesce(hi.lcy_amount,0))                                          actb_history hi       hi.ac_no='0013001600038'  , hi.drcr_ind = 'd'and ah.trn_dt = hi.trn_dt      group hi.ac_no,hi.drcr_ind) total_debits,  ((select  sum( coalesce(hi.lcy_amount,0))                                          actb_history hi       hi.ac_no='0013001600038'  , hi.drcr_ind = 'c' , ah.trn_dt = hi.trn_dt      group hi.ac_no,hi.drcr_ind)      - (select  sum(coalesce(hi.lcy_amount,0))                                          actb_history hi       hi.ac_no='0013001600038'  , hi.drcr_ind = 'd'and ah.trn_dt = hi.trn_dt      group hi.drcr_ind,hi.drcr_ind )) difference  actb_history ah, sttm_cust_account ca ah.ac_no='0013001600038' , ah.ac_no = ca.cust_ac_no group ca.cust_ac_no,  ca.ccy, ah.trn_dt 

the above code calculates difference there corresponding values total_credits , total_debits

the formula credit-debit= difference. problem transactions have 1 , other null. result null after adding nulls.

my desire should display 0 null value , able carry out subtraction.

please help.

it looks me simpler query fit bill:

select ca.cust_ac_no, ca.ccy, ah.trn_dt,       sum(case when ah.drcr_ind='c' ah.lcy_amount else 0 end) total_credits,       sum(case when ah.drcr_ind='d' ah.lcy_amount else 0 end) total_debts,       sum(case when ah.drcr_ind='c' ah.lcy_amount else 0 end)-       sum(case when ah.drcr_ind='d' ah.lcy_amount else 0 end) different    actb_history ah       inner join    sttm_cust_account ca       on          ah.ac_no = ca.cust_ac_no ah.ac_no='0013001600038' group ca.cust_ac_no,  ca.ccy, ah.trn_dt 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -