GROUPS 和 NULL 的 COUNT 加起来不等于 table 中的总行数

COUNT of GROUPS and NULLs don't add up to total rows in the table

GROUPSNULLCOUNT 加起来不等于 table.

中的总行数

我首先想知道,也许 SQL 服务器总行数可能会关闭(我是新手),所以我想通过 运行 a [=12= 进行双重检查] 在 RentalID 列上,然后检查 DISTINCT 值只是为了验证名为 AllData 的 table 中的总行数是否准确。所有结果一致; table 共有 3,471,007 行。

然后我 运行 COUNTGender 列中的所有值使用 GROUP BY,然后分别 运行 一个 COUNT该列中的 NULLs。我预计当我将三者加在一起时,该数字将与 table 中的总行数相匹配,但事实并非如此。相差 3,444。

这是我的代码的副本:

-- HOW MANY TOTAL ROWS IN THE TABLE?  -------------------------------
SELECT  *
FROM AllData
-- 3,471,007 rows 

-- HOW MANY total RentalIDs ARE THERE?  -----------------------------
SELECT RentalID
FROM AllData
-- 3,471,007 rows. 

-- How many UNIQUE RentalIDs ARE THERE?  ----------------------------
SELECT COUNT(DISTINCT(RentalID))
FROM AllData
-- 3,471,007 rows. Means no missing RentalIDs.


-- HOW IS GENDER POPULATED?
SELECT Gender, COUNT(Gender) AS GenderCount
FROM AllData
GROUP BY Gender
/* Results:
   Gender  GenderCount
   NULL    0
   Male    2174743
   Female  737307
*/

-- HOW MANY ROWS ARE MISSING GENDER?  -------------------------------
SELECT  Gender
FROM AllData
WHERE Gender IS NULL
-- 562,401 rows are empty.


/* Why don't these numbers add up to total rows in table?
   2,174,743 Count of Gender = Male
     737,307 Count of Gender = Female
     562,401 Count of Gender = NULL
   3,474,451 This should equal total rows in table, but it doesn't

   3,471,007 Total rows in table
       3,444 Difference: these rows not accounted for...  */
SELECT Gender, COUNT(Gender) AS GenderCount
FROM AllData
GROUP BY Gender

以上语句在性别方面针对 NULL 给出了 0,因为 COUNT 函数不会计算 NULL 值。因此,您可以选择以下任何一种方法

推荐方法

SELECT Gender, COUNT(1) AS GenderCount
FROM AllData
GROUP BY Gender

选择#1

SELECT Gender, COUNT(*) AS GenderCount
    FROM AllData
    GROUP BY Gender

选择#2

SELECT Gender, COUNT(ISNULL(Gender,'')) AS GenderCount
    FROM AllData
    GROUP BY Gender