我正在尝试使用count和a where子句来计数列中的空数。此查询的结果返回计数为0,当表中有nulls for Attr_620_lookup。我可以计算所有行,减去不是零..但是有没有办法直接计数nulls?
SELECT Attr_616 AS Location, COUNT(Attr_620_Lookup)
FROM dbo.AttributesDenormalized AS ad
WHERE (Attr_620_Lookup IS NULL)
GROUP BY Attr_616
当您在COUNT
中指定表达式时,它将计算非编号值。只需使用COUNT(*)
:
SELECT Attr_616 AS Location, COUNT(*)
FROM dbo.AttributesDenormalized AS ad
WHERE (Attr_620_Lookup IS NULL)
GROUP BY Attr_616
除了@dstanley提到的内容外,您还可以使用sum的案例语句:
SELECT SUM(CASE WHEN MyField IS NULL THEN 1 ELSE 0 END) AS TheSum
FROM TheTable
,如果您要处理nulls和non nulls,这将是很棒的,但是,您的Were子句专门要求nulls ...这意味着我的案例语句有点多余,如您已经说明了...WHERE MyField IS NULL