Re: I probably don't understand aggregates.

From: David G Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: I probably don't understand aggregates.
Date: 2014-06-11 22:08:17
Message-ID: 1402524497372-5806896.post@n5.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2014-06-11 22:09:33 Re: I probably don't understand aggregates.
Previous Message David G Johnston 2014-06-11 21:54:21 Re: Need help with this Function. I'm getting an error