Solved

MySQL If statement inside query

Posted on 2012-03-12
2
723 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 27

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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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