Re: [HACKERS] Using aggregate in HAVING

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: PostgreSQL-development <pgsql-hackers(at)postgreSQL(dot)org>
Subject: Re: [HACKERS] Using aggregate in HAVING
Date: 1999-12-29 20:17:28
Message-ID: 386A6C58.C453600D@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 1999-12-29 20:25:27 Re: [HACKERS] Using aggregate in HAVING
Previous Message Bruce Momjian 1999-12-29 19:51:19 Using aggregate in HAVING