Re: Aggregates in WHERE clause? - Mailing list pgsql-sql

From Richard Broersma
Subject Re: Aggregates in WHERE clause?
Date
Msg-id 396486430809101659o4fc3b2a8o6e60a51e611af12b@mail.gmail.com
Whole thread Raw
In response to Aggregates in WHERE clause?  ("Ruben Gouveia" <rubes7202@gmail.com>)
Responses Re: Aggregates in WHERE clause?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-sql
On Wed, Sep 10, 2008 at 4:11 PM, Ruben Gouveia <rubes7202@gmail.com> wrote:

> select employee,count(distinct tasks)
>         from job
>         where greatest(max(last_job_date),max(last_position_date)) <
> 2008-08-28 + integer '1'
>         group by employee;
>
> ERROR:  aggregates not allowed in WHERE clause
>

   select employee,count(distinct tasks)     from job
group by employee  having greatest(max(last_job_date),max(last_position_date))          < 2008-08-28 + integer '1';

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

-- 
Regards,
Richard Broersma Jr.

Visit the Los Angeles PostgreSQL Users Group (LAPUG)
http://pugs.postgresql.org/lapug


pgsql-sql by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Aggregates in WHERE clause?
Next
From: Craig Ringer
Date:
Subject: Re: pg_restore in java connection (auto_commit = false)