Re: Aggregates in WHERE clause?

From: "Richard Broersma" <richard(dot)broersma(at)gmail(dot)com>
To: "Ruben Gouveia" <rubes7202(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Aggregates in WHERE clause?
Date: 2008-09-10 23:59:49
Message-ID: 396486430809101659o4fc3b2a8o6e60a51e611af12b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Sep 10, 2008 at 4:11 PM, Ruben Gouveia <rubes7202(at)gmail(dot)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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Craig Ringer 2008-09-11 02:14:54 Re: pg_restore in java connection (auto_commit = false)
Previous Message Stephan Szabo 2008-09-10 23:19:58 Re: Aggregates in WHERE clause?