I use the following
select TotalCredits - TotalDebits as Difference
from
(
select
(select sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1) as TotalCredits,
(select SUM(totalamount) from Journal where MEMBERID=48 and DEBIT =1) As TotalDebits
) temp
this returns a single field with my difference, the problem is that if the table has no credit but has debit, the temp table contains a NULL value in the TotalCredits field, which prohibits the execution of mathematics. (Vica Versa on has Credits, but no flow rates) I tried coal, but I can't seem to make it work.
rationally, I need to check if:
sum(TOTALAMOUNT) from journal where memberid=48 and CREDIT =1 as TotalCredits is
null then totalcredits = 0 and visa versa
sql server 2008
source
share