COUNT(1) - COUNT(column_name) 是否仅 return 空值计数?

Does COUNT(1) - COUNT(column_name) only return the count of null values?

是否

COUNT(1) - COUNT(column_name) as total_nulls

只有 return 给定列中空值的计数?

我相信它确实如此,但想确定。谢谢

count(1)=count(*)--returns nulls
count(columnname)--won't return nulls

你为什么不试试?

DEMO

CREATE TABLE Table1
    ([Country] varchar(7))
;

INSERT INTO Table1
    ([Country])
VALUES
    ('Germany'),
    ('France'),
    (NULL),
    (NULL),
    ('Spain')
;

SELECT count(*), count(country), count(1)
FROM Table1

是的。一个更合乎逻辑且易于阅读的版本是:

select count(*) from myTable where myColumn is null;

PS:此版本也将受益于现有索引。

另一种方法是使用 SUM。

select sum(case when MyColumn is null then 1 else 0 end)