111

当数据为空并且显示结果时出现警告时出现问题。如何解决这个问题呢?。表中没有数据时如何将空数据更改为0?

这是我的代码:-

SELECT DISTINCT c.username             AS assigner_officer,
                d.description          AS ticketcategory,
                (SELECT Count(closed)
                 FROM   ticket
                 WHERE  assigned_to = c.user_id
                        AND closed IS NOT NULL
                 GROUP  BY assigned_to)closedcases,
                (SELECT Count(closed)
                 FROM   ticket
                 WHERE  assigned_to = c.user_id
                        AND closed IS NULL
                 GROUP  BY assigned_to)opencases
FROM   ticket a
       JOIN ticketlog b
         ON a.ticketid = b.ticketid
       JOIN access c
         ON a.assigned_to = c.user_id
       JOIN ticket_category d
         ON a.cat_code = d.id
       JOIN lookup_department e
         ON a.department_code = e.code 

结果如下所示:-

 Warnings: ---> 
   W (1): Warning: Null value is eliminated by an aggregate or other SET operation.
          <--- 
 assigner_officer     ticketcategory     closedcases     opencases    
 -------------------  -----------------  --------------  ------------ 
 abdulhafiz           Enquiry            (null)          0            
 affan                Enquiry            12              (null)       
 amirul               Enquiry            1               (null)       
 azrul_fahmi          Enquiry            45              0            
 Azwani               Enquiry            (null)          0            
 chai                 Enquiry            4               (null)       
 dalinawati           Enquiry            1               0            
 Emmy                 Complaints         (null)          0            
 Fadhlia              Enquiry            38              0            
 fairulhalif          Others             1               (null)       
 farikh               Enquiry            (null)          0            
 ismailh              Enquiry            28              0            
 izzahanna            Enquiry            (null)          0            
 Kamsuzilawati        Enquiry            1               (null)     
4

6 回答 6

115

您将主要使用COUNTUID 进行汇总。所以

COUNT([uid])将产生警告:

警告:空值被聚合或其他 SET 操作消除。

在与左连接一起使用时,其中计数对象不存在。

在这种情况下使用COUNT(*)也会产生不正确的结果,因为您将计算存在的结果(即父母)的总数。

使用COUNT([uid])IS 是一种有效的计数方式,警告无非是警告。但是,如果您担心,并且想要在这种情况下获得真实的 uid 计数,那么您可以使用:

SUM(CASE WHEN [uid] IS NULL THEN 0 ELSE 1 END) AS [new_count]

这不会为您的查询增加很多开销。(测试 mssql 2008)

于 2013-01-08T13:42:15.950 回答
28

解决此问题的一种方法是关闭警告。

SET ANSI_WARNINGS OFF;
GO
于 2013-05-02T13:59:54.723 回答
17

使用ISNULL(field, 0)它也可以与聚合一起使用:

ISNULL(count(field), 0)

但是,您可能会考虑更改count(field) to count(*)

编辑:

尝试:

closedcases = ISNULL(
   (select count(closed) from ticket       
    where assigned_to = c.user_id and closed is not null       
    group by assigned_to), 0), 

opencases = ISNULL(
    (select count(closed) from ticket 
     where assigned_to = c.user_id and closed is null 
     group by assigned_to), 0),
于 2012-07-08T15:37:46.763 回答
13

你想把函数的ISNULL内部COUNT,而不是外部:

不好:ISNULL(COUNT(field), 0)

好的:COUNT(ISNULL(field, 0))

于 2014-12-10T15:05:40.620 回答
-1

我收到了这个错误;我只是WHERE为在子句中使用的字段添加了一个count子句。它解决了这个问题。注意:如果空值存在,检查它是否对报告很关键,因为它被排除在计数中。

旧查询:

select city, Count(Emp_ID) as Emp_Count 
from Emp_DB
group by city

新查询:

select city, Count(Emp_ID) as Emp_Count 
from Emp_DB
where Emp_ID is not null
group by city
于 2018-12-28T11:32:24.127 回答
-3

如果聚合函数中存在任何 Null 值,您将面临此问题。而不是下面的代码

 SELECT Count(closed)
  FROM   ticket
  WHERE  assigned_to = c.user_id
  AND closed IS NULL

使用喜欢

SELECT Count(ISNULL(closed, 0))
  FROM   ticket
  WHERE  assigned_to = c.user_id
  AND closed IS NULL
于 2017-10-14T08:48:22.213 回答