Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MYSQl using a variable to do sum

How can I use the loanAmount and amountPaid to get the balance.

SELECT (SELECT SUM(loanRepayment.amount) FROM `loanRepayment` WHERE loanRepayment.loanNumber='MMSE22062311' AND loanRepayment.transactionType ='DR')loanAmount,
(SELECT SUM(loanRepayment.amount) FROM `loanRepayment` WHERE loanRepayment.loanNumber='MMSE22062311' AND loanRepayment.transactionType ='CR')amountPaid,

(loanAmount-amountPaid)balance

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT SUM(CASE WHEN transactionType = 'DR' 
                THEN amount
                ELSE 0
                END) loanAmount,
       SUM(CASE WHEN transactionType = 'CR' 
                THEN amount
                ELSE 0
                END) amountPaid,
       SUM(CASE WHEN transactionType = 'DR' 
                THEN amount
                ELSE -amount
                END) balance
FROM loanRepayment
WHERE loanNumber='MMSE22062311' 
  AND transactionType IN ('DR', 'CR')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading