SELECT query with CASE condition and SUM() SELECT query with CASE condition and SUM() sql-server sql-server

SELECT query with CASE condition and SUM()


Select SUM(CASE When CPayment='Cash' Then CAmount Else 0 End ) as CashPaymentAmount,       SUM(CASE When CPayment='Check' Then CAmount Else 0 End ) as CheckPaymentAmountfrom TableOrderPaymentWhere ( CPayment='Cash' Or CPayment='Check' ) AND CDate<=SYSDATETIME() and CStatus='Active';


select CPaymentType, sum(CAmount)from TableOrderPaymentwhere (CPaymentType = 'Cash' and CStatus = 'Active')or (CPaymentType = 'Check' and CDate <= bsysdatetime() abd CStatus = 'Active')group by CPaymentType

Cheers -


To get each sum in a separate column:

Select SUM(IF(CPaymentType='Check', CAmount, 0)) as PaymentAmountCheck,       SUM(IF(CPaymentType='Cash', CAmount, 0)) as PaymentAmountCashfrom TableOrderPaymentwhere CPaymentType IN ('Check','Cash') and CDate<=SYSDATETIME() and CStatus='Active';