Re: I probably don't understand aggregates.

Поиск
Список
Период
Сортировка
От David G Johnston
Тема Re: I probably don't understand aggregates.
Дата
Msg-id 1402524497372-5806896.post@n5.nabble.com
обсуждение исходный текст
Ответ на Re: I probably don't understand aggregates.  (Markus Neumann <markus.neumann@math.uzh.ch>)
Ответы Re: I probably don't understand aggregates.  (Markus Neumann <markus@neumann.ch>)
Список pgsql-novice
Markus Neumann wrote
> After all this:
> SELECT myAggr(x) FROM test;
>
> works.

Because this is an aggregate call with an implicit GROUP BY.  The
final_func, sees 0+0+1, performs the reciprocal, and returns 1


> SELECT myWrapperFunc(x) FROM test;
>
> Division by zero!

Because myWrapperFunction is not an aggregate you end up calling myAggr
three times, once each with for the values 0, 0, 1.  The way myAggr is
written if the final state of the function is 0 a division by zero will
occur.

My privately noted confusion about the lack of a GROUP BY still applies - I
just forgot about the implicit GROUP BY when all output columns are defined
using aggregates.

Try executing:

SELECT myAggr(x), x FROM test GROUP BY x;

This is basically what you are doing when you put the aggregate into the
wrapper.

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/I-probably-don-t-understand-aggregates-tp5806879p5806896.html
Sent from the PostgreSQL - novice mailing list archive at Nabble.com.


В списке pgsql-novice по дате отправления:

Предыдущее
От: David G Johnston
Дата:
Сообщение: Re: Need help with this Function. I'm getting an error
Следующее
От: Tom Lane
Дата:
Сообщение: Re: I probably don't understand aggregates.