Solved

MySQL If statement inside query

Posted on 2012-03-12
2
709 Views
Last Modified: 2012-03-22
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
0
Comment
Question by:khaled salem
2 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 37708644
Try:

select Sum(Credit) as CrSum, Sum(Debit) as DebSum,
IF (CrSum>DebSum, CrSum-DebSum, 0) as creditBalance,
IF (DebSum>crSum, DebSum-CrSum, 0)  as DebitBalance
from ...

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 37709615
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
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Creating and Managing Databases with phpMyAdmin in cPanel.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now