Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL If statement inside query

Posted on 2012-03-12
2
Medium Priority
?
744 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 28

Accepted Solution

by:
Sinisa Vuk earned 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

886 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