Skip to content
Advertisement

How to add “or” in SQL select query?

I am creating a report to get customer wise incoming payment in a particular period. Payment can be received through Cheques, Direct Bank Transfers, Cash. This payments saved in the database in a separate column like cheques in “CheckSum” column,Cash amount in “cash sum column”.

My question is how to get payment amount from any kind of payment type in to a single column. Following query I used to get values and I just add “or” there to get an idea.

select 
    convert(varchar, a.DocDate, 110) as DocDate,
    a.CardCode, a.CardName, d.SlpName, e.CheckNum,
    (CashSum or CreditSum or CheckSum or TrsfrSum) as DocTotal 
from 
    ORCT a
left join 
    RCT2 b on b.DocNum = a.DocNum
left join 
    OINV c on c.DocNum = b.DocEntry
left join 
    OSLP d on d.SlpCode = c.SlpCode
left join 
    RCT1 e on e.DocNum = a.DocEntry

Final output should display as follows

enter image description here

Advertisement

Answer

Following expression should do it (assuming other columns are NULL or zero):

ISNULL(CashSum,0) + ISNULL(CreditSum,0) + ISNULL(CheckSum,0) + ISNULL(TrsfrSum,0) as DocTotal
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement