这个问题在这里已有答案:
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';
我想总结PaymentAmountCheck和PaymentAmountCash,任何人都可以帮助我
您可以使用派生选项添加这两个字段,如下所示:
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;
同样在SQL Server中,您可以使用IIF
因为在你提到的CPaymentType
的where子句中,你可以在sum(CAmount)
中添加select
,它将为你提供PaymentAmountCheck
和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';