【问题标题】:TSQL Count function and distinct missing NULL valuesTSQL Count 函数和不同的缺失 NULL 值
【发布时间】:2012-09-10 22:10:11
【问题描述】:

根据 MSDN,TSQL COUNT(*) 函数在结果中包含任何 NULL 值,除非还使用了 DISTINCT(来源:http://msdn.microsoft.com/en-us/library/ms175997.aspx

但是,在我的查询中,NULL 值被忽略了。为了测试这一点,我创建了一个小表并在其中填充了一些数据:

CREATE TABLE tbl (colA varchar(1), colB varchar(10))
INSERT tbl VALUES
('Y', 'test1'),
('Y', 'test2'),
(null, 'test3'),
(null, 'test4'),
('N', 'test5')

然后我对其进行了以下 2 个查询:

SELECT count(*) FROM tbl
WHERE colA <> 'N'

SELECT DISTINCT colA FROM tbl
WHERE colA <> 'N'

两个结果都忽略 NULL 值。我分别得到 2 和 'Y' 作为结果。我不知道为什么会这样。有人可以帮我介绍一下吗?

在 SQL Fiddle 中模拟的结果:http://sqlfiddle.com/#!3/8f00b/9

【问题讨论】:

    标签: sql database sql-server-2008 tsql count


    【解决方案1】:

    空值很奇怪。

    null &lt;&gt; 'N' 计算结果为 false
    null = 'N' 也计算结果为 false

    你需要显式处理null:

    SELECT count(*) FROM tbl
    WHERE (colA <> 'N') or (colA is null)
    

    【讨论】:

      【解决方案2】:
      declare @tbl as Table (colA varchar(1), colB varchar(10)) 
      insert @tbl values 
        ('Y', 'test1'), ('Y', 'test2'), (null, 'test3'), (null, 'test4'), ('N', 'test5')
      select * from @tbl
      
      select
        count(*) as 'Rows', -- All rows.
        count(colA) as [colA Values], -- Rows where   colA   is not NULL.
        count(colB) as [colB Values],  -- Rows where   colB   is not NULL.
        count(distinct colA) as [Distinct colA], -- Number of distinct values in   colA .
        count(distinct colB) as [Distinct colB], -- Number of distinct values in   colB .
        -- NULL never equals anything, including another NULL.
        case when 42 = NULL then 'Yes' else 'Unknown' end as [Answer Is NULL?],
        case when NULL = NULL then 'Yes' else 'Unknown' end as [NULL = NULL],
        -- Use   is NULL   to explicitly check for   NULL .
        case when NULL is NULL then 'Yes' else 'Unknown' end as [NULL is NULL]
        from @tbl
      

      【讨论】:

      • 最详细的答案。谢谢
      【解决方案3】:

      除 IS NULL 之外的任何与 Null 的比较都将失败。所以 Null = 'N' 和 Null 'N' 都返回 false。

      如果你想包含空值,你需要说

      WHERE colA <> 'N' or colA IS NULL
      

      【讨论】:

        【解决方案4】:

        因为NULL 是未知的,服务器不知道它的值是什么。但是尝试使用IS NULL

        SELECT count(*) 
        FROM tbl
        WHERE colA <> 'N' or  
              colA IS NULL
        

        SQLFiddle Demo

        【讨论】:

        • 嗯。如果该值为空,那么它“可能是 N 或 Y”在逻辑上是如何可能的?如果它为 null,则绝对不是这些值。
        • 这个想法是用null来表示“未知”。如果该值为“未知”,则它可能是 N 或 Y
        • 但它不是“未知的”(可能是 N 或 Y)。 已知是“未定义的”。
        • 从我昨天的发现来看,约翰是对的。 null 我们应该是什么意思并不重要。事实上,SQL 已经定义了 IS/IS NOT 运算符来搜索空值。 =/ 运算符不适用于它。
        【解决方案5】:
         select count(IsNull(colA,'')) as colA, count(colB) as colB from @tbl
        

        应该也能解决问题

        【讨论】:

          【解决方案6】:

          你也可以使用ISNULL函数:

          SELECT count(*) 
          FROM tbl
          WHERE isnull(colA,'F') <> 'N' 
          

          MSDN:http://msdn.microsoft.com/en-us/library/aa933210(v=sql.80).aspx

          【讨论】:

          • 是的,你是对的。我昨天发现它不是 count(*) 函数,而是排除空值的 where 子句。
          猜你喜欢
          • 2013-09-29
          • 1970-01-01
          • 2013-12-13
          • 1970-01-01
          • 1970-01-01
          • 2012-06-22
          • 1970-01-01
          • 2020-02-29
          • 2020-07-22
          相关资源
          最近更新 更多