Re: Aggregate functions not allowed in WHERE clause

From: Christopher Browne <cbbrowne(at)acm(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Aggregate functions not allowed in WHERE clause
Date: 2006-06-13 02:24:43
Message-ID: 87hd2p23o4.fsf@wolfe.cbbrowne.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Quoth gramo(dot)gnu(at)gmail(dot)com (Ricardo Naranjo Faccini):
> I have two tables, Claims and Logs, and I need to fish in for the id of
> any
> claim who have into the logs anything into the fields invoices or
> payments
>
> I think the best way to do this is by mean of:
>
> 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"
>
> Anyone could help me to figure out this task please

You might consider using a HAVING clause to add those constraints at
the grouping level...

select claim_id
from logs
group by claim_id
having sum(logs.invoices) > 0 or sum(logs.payments) > 0;

You might need to have those sums in the outer select...
--
(reverse (concatenate 'string "ofni.secnanifxunil" "@" "enworbbc"))
http://linuxfinances.info/info/finances.html
"Microsoft has world class quality control" -- Arthur Norman

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2006-06-13 02:38:17 Re: Aggregate functions not allowed in WHERE clause
Previous Message Brent Wood 2006-06-13 02:04:42 Re: Help with storing spatial (map coordinates) data?