Re: Average of Array? - Mailing list pgsql-sql

From Pavel Stehule
Subject Re: Average of Array?
Date
Msg-id AANLkTimGlfovzQue7Gj8EHc3aA8gD3aIpKKoZ9amf16k@mail.gmail.com
Whole thread Raw
In response to Average of Array?  (Lee Hachadoorian <lee.hachadoorian@gmail.com>)
Responses Re: Average of Array?  (Lee Hachadoorian <lee.hachadoorian@gmail.com>)
List pgsql-sql
Hello

2010/6/25 Lee Hachadoorian <lee.hachadoorian@gmail.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@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>


pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Average of Array?
Next
From: Lee Hachadoorian
Date:
Subject: Re: Average of Array?