問題描述
這有點哲學問題,我猜...
This is a bit of a philosophical question, I guess...
假設我在 SQL Server 中執(zhí)行此查詢:
Say I execute this query in SQL Server:
SELECT mygroup, COUNT(mycolumn)
FROM mytable
GROUP BY mygroup
計數(shù)列包含 NULL 并且 ANSI_WARNINGS 選項為 ON,所以我得到:
The counted column contains NULLs and the ANSI_WARNINGS option is ON, so I get:
警告:空值被聚合或其他 SET 消除操作.
Warning: Null value is eliminated by an aggregate or other SET operation.
我明白這意味著什么.有很多方法可以修復"這個警告.但是... 為什么它首先要抱怨?
I understand what this means. And there are lots of ways to 'fix' this warning. But... why is it complaining in the first place?
跳過任何空值是所有 COUNT(mycolumn)
都可以!如果我想計算所有行,包括該列的空值,我會使用 COUNT(*)
.我意圖跳過空值不是很清楚嗎?
Skipping any NULLs is all COUNT(mycolumn)
does! If I wanted to count all rows, including NULLs for this column, I would have used COUNT(*)
. Isn't it clear that it's my intention to skip NULLs?
我猜 ANSI 標準要求發(fā)出此警告,即使在這種明顯的情況下也是如此.為什么?
I guess the ANSI standard demands this warning, even in this obvious case. Why?
推薦答案
警告僅在聚合結果集中存在實際空值時出現(xiàn).我相信它是為了提醒您不會計算空值,并在您遇到數(shù)據(jù)一致性問題時警告您,因為空值出現(xiàn)在您意想不到的地方.
The warning only appears when an actual null is present in the aggregate resultset. I believe it is there to remind you that nulls will not be counted and to warn you should you come across data consistency issues because a null appears where you did not expect.
這篇關于為什么要警告 COUNT(col) 中的 NULL?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!