Aggregates in WHERE clause?
Aggregates in WHERE clause?
От:
"Ruben Gouveia" <rubes7202@gmail.com>
Дата:
I tried to do the following and got the following error message:
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
********** Error **********
ERROR: aggregates not allowed in WHERE clause
SQL state: 42803
Is there away around this? Should i create a function to populate a variable that can be used in it's place...will that even work?
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
********** Error **********
ERROR: aggregates not allowed in WHERE clause
SQL state: 42803
Is there away around this? Should i create a function to populate a variable that can be used in it's place...will that even work?
Re: Aggregates in WHERE clause?
От:
Stephan Szabo <sszabo@megazone.bigpanda.com>
Дата:
On Wed, 10 Sep 2008, Ruben Gouveia wrote: > I tried to do the following and got the following error message: > > 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 You probably want to look at some variant with HAVING, assuming you intend those max()s to be based on the employee groups.
Re: Aggregates in WHERE clause?
От:
"Richard Broersma" <richard.broersma@gmail.com>
Дата:
On Wed, Sep 10, 2008 at 4:11 PM, Ruben Gouveia 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
Re: Aggregates in WHERE clause?
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
"Richard Broersma" writes: > On Wed, Sep 10, 2008 at 4:11 PM, Ruben Gouveia 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
Re: Aggregates in WHERE clause?
От:
"Igor Neyman" <ineyman@perceptron.com>
Дата:
select employee,count(distinct tasks), greatest(max(last_job_date),max(last_position_date))
from job
group by employee
from job
group by employee
having greatest(max(last_job_date),max(last_position_date)) < 2008-08-28 + integer '1';
From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of Ruben Gouveia
Sent: Wednesday, September 10, 2008 7:11 PM
To: pgsql-sql
Subject: [SQL] Aggregates in WHERE clause?
I tried to do the following and got the following error message:
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
********** Error **********
ERROR: aggregates not allowed in WHERE clause
SQL state: 42803
Is there away around this? Should i create a function to populate a variable that can be used in it's place...will that even work?
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
********** Error **********
ERROR: aggregates not allowed in WHERE clause
SQL state: 42803
Is there away around this? Should i create a function to populate a variable that can be used in it's place...will that even work?