Re: I probably don't understand aggregates. - Mailing list pgsql-novice

From David G Johnston
Subject Re: I probably don't understand aggregates.
Date
Msg-id 1402524497372-5806896.post@n5.nabble.com
Whole thread Raw
In response to Re: I probably don't understand aggregates.  (Markus Neumann <markus.neumann@math.uzh.ch>)
Responses Re: I probably don't understand aggregates.  (Markus Neumann <markus@neumann.ch>)
List 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 by date:

Previous
From: David G Johnston
Date:
Subject: Re: Need help with this Function. I'm getting an error
Next
From: Tom Lane
Date:
Subject: Re: I probably don't understand aggregates.