The information in this article applies to:
SUMMARYWhen both aggregate functions and table columns are included in the select list of a query without the GROUP BY clause, the WHERE clause applies only to the rows included in the calculation of the aggregate function, but does not restrict the rows returned by the query. Similarly, if a HAVING clause is used without a GROUP BY clause (which is proper in SQL Server), a HAVING clause restricts the rows returned by the query but does not affect the calculation of the aggregate function. MORE INFORMATION
Consider the following query
which returns the following results:
Note that the condition in the WHERE clause does not restrict the rows returned. Similarly, the following query
returns the following results:
Note also that the condition in the HAVING clause does not affect the calculation of the aggregate function. If both WHERE and HAVING are used, then both the returned rows and the calculation of the aggregate function are restricted. The following query shows this clearly:
This query returns:
The above properties of the WHERE and HAVING clauses also apply to all other types of aggregate functions. Although it is generally considered meaningless to mix column names with aggregate functions in the select list without GROUP BY, it is used in practice anyway. Therefore, special caution should be taken when using these types of queries because users can easily misinterpret their meaning and get "unexpected" query results. Additional query words: Windows NT dblib
Keywords : kbprg SSrvDB_Lib SSrvGen SSrvTrans SSrvWinNT |
Last Reviewed: March 11, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |