Re: Aggregate functions not allowed in WHERE clause

Поиск
Список
Период
Сортировка
От Christopher Browne
Тема Re: Aggregate functions not allowed in WHERE clause
Дата
Msg-id 87hd2p23o4.fsf@wolfe.cbbrowne.com
обсуждение исходный текст
Ответ на Aggregate functions not allowed in WHERE clause  (Ricardo Naranjo Faccini <gramo.gnu@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: Michael Fuhr
Дата:
Сообщение: Re: Aggregate functions not allowed in WHERE clause
Следующее
От: John Tregea
Дата:
Сообщение: Re: Help with storing spatial (map coordinates) data?