問(wèn)題描述
這有點(diǎn)哲學(xué)問(wèn)題,我猜...
This is a bit of a philosophical question, I guess...
假設(shè)我在 SQL Server 中執(zhí)行此查詢:
Say I execute this query in SQL Server:
SELECT mygroup, COUNT(mycolumn)
FROM mytable
GROUP BY mygroup
計(jì)數(shù)列包含 NULL 并且 ANSI_WARNINGS 選項(xiàng)為 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.
我明白這意味著什么.有很多方法可以修復(fù)"這個(gè)警告.但是... 為什么它首先要抱怨?
I understand what this means. And there are lots of ways to 'fix' this warning. But... why is it complaining in the first place?
跳過(guò)任何空值是所有 COUNT(mycolumn)
都可以!如果我想計(jì)算所有行,包括該列的空值,我會(huì)使用 COUNT(*)
.我意圖跳過(guò)空值不是很清楚嗎?
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 標(biāo)準(zhǔn)要求發(fā)出此警告,即使在這種明顯的情況下也是如此.為什么?
I guess the ANSI standard demands this warning, even in this obvious case. Why?
推薦答案
警告僅在聚合結(jié)果集中存在實(shí)際空值時(shí)出現(xiàn).我相信它是為了提醒您不會(huì)計(jì)算空值,并在您遇到數(shù)據(jù)一致性問(wèn)題時(shí)警告您,因?yàn)榭罩党霈F(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.
這篇關(guān)于為什么要警告 COUNT(col) 中的 NULL?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!