Re: Aggregates not allowed in WHERE clause?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Frans <fransgunawan(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Aggregates not allowed in WHERE clause?
Date: 2004-12-16 05:37:19
Message-ID: 20041216053719.GA43376@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, Dec 16, 2004 at 12:02:34PM +0700, Frans wrote:

> I try to use : select name, sum(quantity) from info where
> sum(quantity)>20 group by name;
> This yields the message: 'Aggregates not allowed in WHERE clause'.

For aggregates use HAVING, not WHERE:

SELECT name, SUM(quantity)
FROM info
GROUP BY name
HAVING SUM(quantity) > 20;

http://www.postgresql.org/docs/7.4/static/tutorial-agg.html
http://www.postgresql.org/docs/7.4/static/queries-table-expressions.html#QUERIES-GROUP
http://www.postgresql.org/docs/7.4/static/sql-select.html

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Christopher Browne 2004-12-16 06:22:45 Re: Aggregates not allowed in WHERE clause?
Previous Message Michael Fuhr 2004-12-16 05:22:07 Re: bytea internal encoding