Re: Aggregate functions not allowed in WHERE clause - Mailing list pgsql-general

From Christopher Browne
Subject Re: Aggregate functions not allowed in WHERE clause
Date
Msg-id 87hd2p23o4.fsf@wolfe.cbbrowne.com
Whole thread Raw
In response to Aggregate functions not allowed in WHERE clause  (Ricardo Naranjo Faccini <gramo.gnu@gmail.com>)
List pgsql-general
Quoth gramo.gnu@gmail.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

pgsql-general by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: Aggregate functions not allowed in WHERE clause
Next
From: John Tregea
Date:
Subject: Re: Help with storing spatial (map coordinates) data?