Home All Groups Group Topic Archive Search About

Aggregate Functions - and no records

Author
21 Oct 2005 8:41 AM
Rebecca York
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

Author
21 Oct 2005 10:40 AM
Erland Sommarskog
Rebecca York (rebecca.york {at} 2ndbyte.com) writes:
Show quote
> 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, esq***@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp
Author
21 Oct 2005 10:40 AM
Rebecca York
Phew :)

Show quote
"Erland Sommarskog" <esq***@sommarskog.se> wrote in message
news:Xns96F6808DFCEA4Yazorman@127.0.0.1...
> Rebecca 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.

AddThis Social Bookmark Button