Find the balance amount (sum(credit) – sum(debit)) from the following table?
Ans: find out the balance amount of account no: 1
select a.SumCr as Credit ,
b.SumDr as Debit ,
a.SumCr - b.SumDr as Balance
from ( select sum(A_Amount) as SumCr
from tblAccount
where A_ID = 1
and A_Type = 'Cr'
) a ,
( select sum(A_Amount) as SumDr
from tblAccount
where A_ID = 1
and A_Type = 'Dr'
) b
Ans: find out the balance amount for all the account numbers in the table:
select a.A_ID as ID ,
a.SumCr as Credit ,
b.SumDr as Debit ,
a.SumCr - b.SumDr as Balance
from ( select A_ID ,
sum(A_Amount) as SumCr
from tblAccount
where A_Type = 'Cr'
group by A_ID
) a
inner join ( select A_ID ,
sum(A_Amount) as SumDr
from tblAccount
where A_Type = 'Dr'
group by A_ID
) b on a.A_ID = b.A_ID

hmm. really like it ))