缺失数据的总和

时间:2014-06-25 16:27:11

标签: sql-server tsql

以下查询针对上周的总订单显示网站。 但如果在上周没有给定网站的订单,我仍然应该看到该网站的总和为零。 目前它只给我四个网站,因为这些网站上周没有订单。

select SITE
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as COMPLETED
from 
( 
   SELECT DISTINCT ORDERS.SITE, ORDERS.ORDERDATE  FROM ORDERS
   INNER JOIN PHONEDATA AS P
   ON ORDERS.RECID = P.OrderID
   where SITE IN ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7')

) X
GROUP BY SITE
order by SITE

结果:

网站---------------------完成

SITE1 ----------------------- 2

SITE2 ----------------------- 2

SITE3 ----------------------- 2

SITE4 ----------------------- 2

预期结果:

网站---------------------完成

SITE1 ----------------------- 2

SITE2 ----------------------- 2

SITE3 ----------------------- 2

SITE4 ----------------------- 2

SITE5 ----------------------- 0

SITE6 ----------------------- 0

SITE7 ----------------------- 0

更新

select SITE
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as COMPLETED
from 
( 
   SELECT DISTINCT ORDERS.SITE, ORDERS.ORDERDATE  FROM ORDERS

   where SITE IN ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7')

) X
GROUP BY SITE
order by SITE

我现在已经删除了带有电话数据表的内部联接,所以我现在得到了丢失的网站。但我避免使用这种方法的原因是因为如果我只依赖于订单表,订单日期时间字段会为给定订单插入几次,最后的订单会进入phonedata表,所以现在我在完成的计数中得到更多的值但它应该只考虑每个站点每天的最新值

更新结果:

网站---------------------完成

SITE1 ----------------------- 5

SITE2 ----------------------- 5

SITE3 ----------------------- 5

SITE4 ----------------------- 5

SITE5 ----------------------- 0

SITE6 ----------------------- 0

SITE7 ----------------------- 0

预期

网站---------------------完成

SITE1 ----------------------- 2

SITE2 ----------------------- 2

SITE3 ----------------------- 2

SITE4 ----------------------- 2

SITE5 ----------------------- 0

SITE6 ----------------------- 0

SITE7 ----------------------- 0

3 个答案:

答案 0 :(得分:0)

如果表中没有没有订单的网站的行,那么它如何返回任何要计数的行?也许你有一张桌子上有可以加入的所有可能的网站?或者使用站点值创建临时表。然后,您可以将订单表连接到此处。即。

create table #sites (site varchar(25));
insert into #sites values ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7');
...
from 
( 
   SELECT DISTINCT ORDERS.SITE, ORDERS.ORDERDATE  FROM 
   #sites s left join ORDERS on orders.site = s.site
   INNER JOIN PHONEDATA AS P
   ON ORDERS.RECID = P.OrderID

) X
...

答案 1 :(得分:0)

尝试使用左连接而不是内连接。它可能没有从手机数据表中获取行:

select SITE
    ,SUM(Case When  OrderDate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
              Then 1
            Else 0 
            End) as COMPLETED
from 
( 
   SELECT DISTINCT ORDERS.SITE, ORDERS.ORDERDATE  FROM ORDERS
   Left JOIN PHONEDATA AS P
   ON ORDERS.RECID = P.OrderID
   where SITE IN ('SITE1','SITE2','SITE3','SITE4','SITE5','SITE6','SITE7')

) X
GROUP BY SITE
order by SITE

答案 2 :(得分:0)

最好从“网站”表开始,然后加入您的搜索结果。此示例模仿行为,可用作黑客解决方法。

DECLARE @table TABLE
    (
      site VARCHAR(10) ,
      Completed TINYINT
    )
INSERT  INTO @table
        ( site, Completed )
VALUES  ( 'SITE1', 0 ),
        ( 'SITE2', 0 ),
        ( 'SITE3', 0 ),
        ( 'SITE4', 0 ),
        ( 'SITE5', 0 ),
        ( 'SITE6', 0 ),
        ( 'SITE7', 0 )

WITH    cte
          AS ( SELECT   SITE ,
                        SUM(CASE WHEN OrderDate >= DATEADD(dd,( DATEDIFF(dd, -53690, GETDATE() - 1) / 7 ) * 7, -53690)
                                 THEN 1
                                 ELSE 0
                            END) AS COMPLETED
               FROM     ( SELECT DISTINCT
                                    ORDERS.SITE ,
                                    ORDERS.ORDERDATE
                          FROM      ORDERS
                                    INNER JOIN PHONEDATA AS P ON ORDERS.RECID = P.OrderID
                          WHERE     SITE IN ( 'SITE1', 'SITE2', 'SITE3',
                                              'SITE4', 'SITE5', 'SITE6',
                                              'SITE7' )
                        )
               GROUP BY SITE
             )
    SELECT  t.site ,
            t.completed + cte.COMPLETED
    FROM    @table t
            LEFT OUTER JOIN cte ON t.site = cte.Site
    ORDER BY t.site