Solved

MySQL If statement inside query

Posted on 2012-03-12
2
707 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:khaledsalem
2 Comments
 
LVL 25

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 31

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

11 Experts available now in Live!

Get 1:1 Help Now