Re: Aggregate functions not allowed in WHERE clause

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: 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 02:38:17
Message-ID: 20060613023817.GA3333@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John Tregea 2006-06-13 02:45:22 Re: Help with storing spatial (map coordinates) data?
Previous Message Christopher Browne 2006-06-13 02:24:43 Re: Aggregate functions not allowed in WHERE clause