案件陈述总和[重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

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,任何人都可以帮助我

sql sql-server-2014
2个回答
0
投票

您可以使用派生选项添加这两个字段,如下所示:

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


0
投票

因为在你提到的CPaymentType的where子句中,你可以在sum(CAmount)中添加select,它将为你提供PaymentAmountCheckPaymentAmountCash的总和。

  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';
© www.soinside.com 2019 - 2024. All rights reserved.