Re: [HACKERS] Using aggregate in HAVING - Mailing list pgsql-hackers

From Mike Mascari
Subject Re: [HACKERS] Using aggregate in HAVING
Date
Msg-id 386A6C58.C453600D@mascari.com
Whole thread Raw
In response to Using aggregate in HAVING  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: [HACKERS] Using aggregate in HAVING
List pgsql-hackers
Bruce Momjian wrote:
> 
> How would I get all friends greater than the average age?
> 
>         CREATE TABLE friends (
>                  firstname CHAR(15),
>                  lastname CHAR(20),
>                  age INTEGER)
> 
>         SELECT firstname, lastname
>         FROM friends
>         HAVING age >= AVG(age)
> 
>         ERROR:  Attribute friends.firstname must be GROUPed or used in an
>         aggregate function
> 
> This fails too:
> 
>         SELECT firstname, lastname
>         FROM friends
>         WHERE age >= AVG(age)
> 
>         ERROR:  Aggregates not allowed in WHERE clause
> 
> This fails.  I am stumped.

Without using subselects? With subselects you could also do:

SELECT firstname, lastname
FROM friends
WHERE age >= (SELECT AVG(age) FROM friends);

Are you writing the chapter on aggregates? 

Mike Mascari


pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Using aggregate in HAVING
Next
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] Using aggregate in HAVING