Re: Average of Array?

From: Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>
To: pgsql-sql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Average of Array?
Date: 2010-06-25 16:34:01
Message-ID: AANLkTinW2Z7lBrPkMw9HIv7ZkGL-goMOSpBwRj2ToiIv@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thanks Tom & Pavel, these are very helpful.

On Fri, Jun 25, 2010 at 12:29 PM, Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> wrote:
> Hello
>
> 2010/6/25 Lee Hachadoorian <lee(dot)hachadoorian(at)gmail(dot)com>:
>> Is there a function that returns the average of the elements of an
>> array? I'm thinking of something that would work like the avg()
>> aggregate function where it returns the average of all non-NULL
>> values. Can't find anything like it in the docs, and I'd like to make
>> sure I'm not missing something.
>
> it doesn't exists, but it is simple to develop it
>
> CREATE OR REPLACE FUNCTION array_avg(double precision[])
> RETURNS double precision AS $$
> SELECT avg(v) FROM unnest($1) g(v)
> $$ LANGUAGE sql;
>
> Regards
>
> Pavel Stehule
>
>>
>> Thanks,
>>
>> --
>> Lee Hachadoorian
>> PhD Student, Geography
>> Program in Earth & Environmental Sciences
>> CUNY Graduate Center
>>
>> --
>> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-sql
>>
>

--
Lee Hachadoorian
PhD Student, Geography
Program in Earth & Environmental Sciences
CUNY Graduate Center

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Geery 2010-06-25 17:52:40 enforcing constraints across multiple tables
Previous Message Pavel Stehule 2010-06-25 16:29:10 Re: Average of Array?