Skip site navigation (1) Skip section navigation (2)

Re: Aggregates in WHERE clause?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Richard Broersma" <richard(dot)broersma(at)gmail(dot)com>
Cc: "Ruben Gouveia" <rubes7202(at)gmail(dot)com>, pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Aggregates in WHERE clause?
Date: 2008-09-11 12:03:23
Message-ID: 16867.1221134603@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-sql
"Richard Broersma" <richard(dot)broersma(at)gmail(dot)com> writes:
> On Wed, Sep 10, 2008 at 4:11 PM, Ruben Gouveia <rubes7202(at)gmail(dot)com> wrote:
>> ERROR:  aggregates not allowed in WHERE clause

> Having clause works on agregates.  If you want to force it in the
> where, you need to put the groupby in a sub-query.

The way I like to think about it is "WHERE filters rows before aggregate
functions are computed; HAVING filters them afterwards".  Seen in those
terms, it's obvious why WHERE can't contain any aggregates.

			regards, tom lane

In response to

pgsql-sql by date

Next:From: Igor NeymanDate: 2008-09-11 12:43:04
Subject: Re: Aggregates in WHERE clause?
Previous:From: Craig RingerDate: 2008-09-11 02:14:54
Subject: Re: pg_restore in java connection (auto_commit = false)

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group