考虑以下:
db=> SELECT ga, count(ga) FROM gb GROUP BY ga ORDER BY ga ;
Run Code Online (Sandbox Code Playgroud)
这给出了以下输出:
ga | count
---+------
? | 200
A | 100
B | 50
| 0
(4 rows)
Run Code Online (Sandbox Code Playgroud)
为什么还有一个空行ga
且计数为零?
为什么有一个空 ga 且计数为零的附加行?
因为列中有NULL
值ga
。
为什么计数为零?
WhileCOUNT(*)
永远不能给出 0 作为结果(因为它计算行数),COUNT(column)
或者COUNT(expression)
可以给出 0 因为它计算列/表达式中的非空值。
因此,唯一合乎逻辑的解释是该ga
列有一些空值。
多少?
您可以通过运行找到:
SELECT ga,
count(ga) AS count_non_nulls,
count(*) AS count_all
FROM gb
GROUP BY ga
ORDER BY ga ;
Run Code Online (Sandbox Code Playgroud)
这会给你类似的东西:
ga | count_non_nulls | count_all
---+-----------------+-----------
? | 200 | 200
A | 100 | 100
B | 50 | 50
| 0 | 17 -- some number >= 1
(4 rows)
Run Code Online (Sandbox Code Playgroud)