使用CASE语句的2行之和

时间:2018-07-12 07:12:19

标签: sql sql-server sql-server-2008

我想显示 Subscriptional 行的SUM,并显示为一个 Subscriptional 。我的脚本显示如下:

enter image description here

DECLARE @DATENOW datetime
SET @DATENOW = Convert(DateTime, DATEDIFF(DAY, 0, GETDATE()))
use ews
Select CASE
    WHEN ot.PaymentType=0 
    THEN 'Transactional - '
    ELSE 'Subscriptional - '
    End as 'Payment Type',
          count (distinct o.OrderNumber) as 'Count' from    [order] AS o WITH (NOLOCK) LEFT OUTER JOIN
      OrderProducts AS op WITH (NOLOCK) ON o.OrderNumber = op.OrderNumber LEFT OUTER JOIN
      OrderTransaction AS ot WITH (NOLOCK) ON o.OrderNumber = ot.OrderNumber
    where  o.Datecreated >=DATEADD(D,-1,@DATENOW ) 
    AND o.Datecreated <=DATEADD(ms,-3,@DATENOW )
    AND o.status not like'%Deleted%'
    group  by ot.PaymentType
    UNION
     SELECT 'Deleted', count(*)
     from   [order] AS o WITH (NOLOCK) LEFT OUTER JOIN
                    OrderProducts AS op WITH (NOLOCK) ON o.OrderNumber = op.OrderNumber LEFT OUTER JOIN
                    OrderTransaction AS ot WITH (NOLOCK) ON o.OrderNumber = ot.OrderNumber
                   where  o.Datecreated >=DATEADD(D,-1,@DATENOW ) 
    AND o.Datecreated <=DATEADD(ms,-3,@DATENOW )
    and o.status like'%Deleted%'
     group  by ot.paymenttype

enter image description here

这是原始结果,它表明事务性STD和WB在第二个结果上相加,而Subscriptional SCR和SSR则不是。我希望对第二个结果进行总结,包括订阅SSR和SCR。


订阅-6
交易性-26

3 个答案:

答案 0 :(得分:0)

我将原始查询更改为子查询,并在子查询后将组移开。固定CountOrder列的总和。

SELECT [Payment Type], SUM([CountOrder]) FROM (
SELECT CASE 
         WHEN ot.paymenttype = 0 THEN 'Transactional - ' 
         ELSE 'Subscriptional - ' 
       END                            AS [Payment Type], 
       Count (DISTINCT o.ordernumber) AS [CountOrder]
FROM   [order] AS o WITH (nolock) 
       LEFT OUTER JOIN orderproducts AS op WITH (nolock) 
                    ON o.ordernumber = op.ordernumber 
       LEFT OUTER JOIN ordertransaction AS ot WITH (nolock) 
                    ON o.ordernumber = ot.ordernumber 
WHERE  o.datecreated >= Dateadd(d, -1, @DATENOW) 
       AND o.datecreated <= Dateadd(ms, -3, @DATENOW) 
       AND o.status NOT LIKE'%Deleted%' 
UNION 
SELECT 'Deleted' AS [Payment Type], 
       Count(*) AS [CountOrder]
FROM   [order] AS o WITH (nolock) 
       LEFT OUTER JOIN orderproducts AS op WITH (nolock) 
                    ON o.ordernumber = op.ordernumber 
       LEFT OUTER JOIN ordertransaction AS ot WITH (nolock) 
                    ON o.ordernumber = ot.ordernumber 
WHERE  o.datecreated >= Dateadd(d, -1, @DATENOW) 
       AND o.datecreated <= Dateadd(ms, -3, @DATENOW) 
       AND o.status LIKE'%Deleted%' 
       )

GROUP BY [Payment Type]

答案 1 :(得分:0)

在第二个SELECT块中,使用count(*)而不是count(o.OrderNumber)进行检查。

答案 2 :(得分:0)

问题可能是因为您在第一个联合选择中按ot.PaymentType分组。
对于订阅而言,ot.PaymentType对于不同的订阅而言是不同的。

因此,您可以确保将其按PaymentType的每个集合进行分组。
然后在CASE WHEN中也使用它。

 ...
 case when IIF(ot.PaymentType=0,0,1) = 0 then 'Transactional -' else 'Subscriptional - ' end as [Payment Type]
 ...
 group by IIF(ot.PaymentType=0,0,1)
 ...

展示其功能的简化示例:

declare @OrderTransaction table (OrderId int identity(1,1), PaymentType int);
insert into @OrderTransaction (PaymentType) values 
(0),(0),(1),(2),(2),(3),(3),(3);

-- How it shouldn't be
select 
 case when ot.PaymentType = 0 then 'Transactional -' else 'Subscriptional - ' end as [Payment Type], 
 count(distinct ot.OrderId) as [Count]
from @OrderTransaction ot
group by ot.PaymentType;

-- How it could be
select 
 case when IIF(ot.PaymentType=0,0,1) = 0 then 'Transactional -' else 'Subscriptional - ' end as [Payment Type], 
 count(distinct ot.OrderId) as [Count]
from @OrderTransaction ot
group by IIF(ot.PaymentType=0,0,1);