Avatar of khaled salem
khaled salem
Flag for United States of America asked on

MySQL If statement inside query

I'm using Delphi 2010 & MySQL in an application,  I am facing a problem  to calculate the sum of Services Debit Side, and the payment at credit side in one table, I'd like to get the Balance sheet. according to the Sum(Credit)-Sum(Debit).

IF (Sum(Credit)>Sum(Debit) , Sum(Credit)-Sum(Debit), 0)  as creditBalance
IF (Sum(Debit>Sum(Credit), , Sum(Debit)-Sum(Credit), 0)  as DebitBalance
queryif.png
DelphiMySQL ServerSQL

Avatar of undefined
Last Comment
awking00

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Sinisa Vuk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
awking00

select ...
case when sum(credit) > sum(debit)
     then sum(credit) - sum(debit)
     else 0
end as creditbalance,
case when sum(debit) > sum(credit)
     then sum(debit) - sum(credit)
     else 0
end as debitbalance
Your help has saved me hundreds of hours of internet surfing.
fblack61