ACC2000: Count Function Ignores Null ValuesID: Q201982
|
When you use the Count function in a query, view, or stored procedure, Null values are ignored.
The resolution to this problem is different depending on whether you are working with a query in a Microsoft Access database (.mdb), or with views and stored procedures in a Microsoft Access project (.adp).
Column1 Column2
------- -------
apples
oranges
cherries junk
null
notnull junk
Query: Query1
-------------
Type: Select Query
Field: Column2
Table: Table1
Total: GroupBy
Field: Expr1: NZ([Column2])
Table: Table1
Total: Count
Column2 Expr1
------- -----
3
junk 2
COALESCE(expression 1, expression 2, ..., expression-n)where each expression will evaluate to either NULL or a value.
SELECT Column2, COUNT(COALESCE([Column2], 0)) As Expr1
FROM Table1
GROUP BY Column2
In this case, the COALESCE statement will return the value of Column2 if it is not NULL. If Column2 is NULL, it will return a zero, which will be used by the COUNT statement.
Query: Query1
-------------
Type: Select Query
Field: Column2
Table: Table1
Total: GroupBy
Field: Column2
Table: Table1
Total: Count
Column2 Count(Column2)
------- --------------
0
junk 2
For more information about aggregate functions including the Count function, click Microsoft Access Help on the
Help menu, type "perform calculations in a query" in the Office Assistant or the Answer Wizard,
and then click Search to view the topics returned.
For more information about the COALESCE statement, search for "coalesce" in the SQL Server 7.0 Books Online.
The SQL Server 7.0 Books Online are available from the Microsoft Web site at:
http://support.microsoft.com/download/support/mslfiles/sqlbol.exe
Additional query words: prb
Keywords : kbusage kbdta QryOthr
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbprb
Last Reviewed: May 13, 1999