Thursday, March 8, 2012

Aggregate Functions - and no records

Is the following statement documented/supported?
I can't find it documented in BOL the results for Aggregate functions when
there is no input data.
If a Aggregate function gets 0 records input, it returns null.
eg.
SELECT
MAX( Field ) AS ReturnsNull
FROM
(
SELECT TOP 0 1 AS Field
) NoRecordsRebecca York (rebecca.york {at} 2ndbyte.com) writes:
> Is the following statement documented/supported?
> I can't find it documented in BOL the results for Aggregate functions when
> there is no input data.
>
> If a Aggregate function gets 0 records input, it returns null.
> eg.
> SELECT
> MAX( Field ) AS ReturnsNull
> FROM
> (
> SELECT TOP 0 1 AS Field
> ) NoRecords
It is actually in Books Online, but it was not too easy to find it. Try
Accessing and Changing Relational Data
Query Fundamentals
Advanded Query Concepts
Using Aggregate Functions
Null Values
Here is says:
If no rows meet the condition(s) specified in the WHERE clause, COUNT
returns a value of zero. The other functions all return NULL. COUNT(*),
counts each row, even if all column values are NULL.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Phew :)
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns96F6808DFCEA4Yazorman@.127.0.0.1...
> Rebecca York (rebecca.york {at} 2ndbyte.com) writes:
when
> It is actually in Books Online, but it was not too easy to find it. Try
> Accessing and Changing Relational Data
> Query Fundamentals
> Advanded Query Concepts
> Using Aggregate Functions
> Null Values
> Here is says:
> If no rows meet the condition(s) specified in the WHERE clause, COUNT
> returns a value of zero. The other functions all return NULL. COUNT(*),
> counts each row, even if all column values are NULL.

No comments:

Post a Comment