Re: Aggregate functions not allowed in WHERE clause

From: pradeep singh <lets_begin_with_me(at)yahoo(dot)com>
To: Michael Fuhr <mike(at)fuhr(dot)org>, Ricardo Naranjo Faccini <gramo(dot)gnu(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Aggregate functions not allowed in WHERE clause
Date: 2006-06-13 03:40:29
Message-ID: 20060613034029.30994.qmail@web52514.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

i think this query can be rewritten as

SELECT claim_id,sum(invoices),sum(payments)
FROM logs
GROUP BY claim_id
HAVING sum(invoices) > 0 OR sum(payments) > 0;

having clause can be used with aggregate functions but
those functions should be the part of column
list/expression list in the SELECT statement.

pradeep

--- Michael Fuhr <mike(at)fuhr(dot)org> wrote:

> On Mon, Jun 12, 2006 at 09:00:33PM -0500, Ricardo
> Naranjo Faccini wrote:
> > SELECT claim_id
> > FROM logs
> > WHERE (
> > sum(logs.invoices) > 0
> > OR
> > sum(logs.payments) > 0
> > )
> > GROUP BY claim_id
> >
> > But Postgres claims "Aggregate functions not
> allowed in WHERE clause"
>
> I think you're looking for HAVING. Does the
> following do what you
> want?
>
> SELECT claim_id
> FROM logs
> GROUP BY claim_id
> HAVING sum(invoices) > 0 OR sum(payments) > 0;
>
> --
> Michael Fuhr
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Meskes 2006-06-13 06:57:28 Re: ECPG and Curors.
Previous Message John Tregea 2006-06-13 03:26:29 Re: Help with storing spatial (map coordinates) data?