0
Select SUM(IF(CPaymentType='Check', CAmount, 0)) as PaymentAmountCheck, 
       SUM(IF(CPaymentType='Cash', CAmount, 0)) as PaymentAmountCash 
from TableOrderPayment 
where CPaymentType IN ('Check','Cash') and CDate<=SYSDATETIME() and CStatus='Active';

I want to sum PaymentAmountCheck and PaymentAmountCash, can anyone help me please

juergen d
  • 201,996
  • 37
  • 293
  • 362

2 Answers2

0

You can add those two fields with derived option like the below:

SELECT PaymentAmountCheck, 
      PaymentAmountCash, 
      PaymentAmountCheck + PaymentAmountCash AS TotalAmount
FROM (
 Select SUM(IIF(CPaymentType='Check', CAmount, 0)) as PaymentAmountCheck, 
       SUM(IIF(CPaymentType='Cash', CAmount, 0)) as PaymentAmountCash,
 from TableOrderPayment 
 where CPaymentType IN ('Check','Cash') and CDate<=SYSDATETIME() and CStatus='Active';
) AS Q;

Also in SQL Server, you may use IIF

Arulkumar
  • 12,966
  • 14
  • 47
  • 68
0

Since in the where clause you have mentioned CPaymentType you can just add sum(CAmount) in select that will give you sum of PaymentAmountCheck and PaymentAmountCash .

  Select SUM(IF(CPaymentType='Check', CAmount, 0)) as PaymentAmountCheck, 
           SUM(IF(CPaymentType='Cash', CAmount, 0)) as PaymentAmountCash ,
           sum(CAmount) total
    from TableOrderPayment 
    where CPaymentType IN ('Check','Cash') and CDate<=SYSDATETIME() and CStatus='Active';
Rima
  • 1,447
  • 1
  • 6
  • 12