Peter88tom Peter88tom - 3 months ago 10
MySQL Question

How to select while comparing two columns from the same table in sql

CREATE VIEW mainT AS SELECT transactionId AS accountId, glbalance AS debit IF(totalDebit > totalCredit),glbalance as Credit IF(totalCredit > totalDebit) FROM `trialbalanceView`


Is it possible to generate this kind of query in sql?. Please any help, 'll be appriciated.

Answer

You are wanting a CASE statement it seems like...

CREATE VIEW mainT 
AS 
SELECT 
    transactionId AS accountId, 
    glbalance AS debit,
    case when totalDebit > totalCredit then glbalance else null end as Debit,
    case when totalCredit > totalDebit then glbalance else null end as Credit
FROM trialbalanceView