avg() of array values

From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: avg() of array values
Date: 2007-09-11 15:50:38
Message-ID: 46E6B94E.70607@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

I'm trying to get an avg value of 2 dates (to get to the month that most
part of an interval is in). I found SP's to generate rows from array
values, which I figured I could use with the avg aggregate, but to my
surprise:

*> create or replace function explode_array(in_array anyarray) returns
setof anyelement as
-> $$
$>
$> select ($1)[s] from generate_series(1,array_upper($1, 1)) as s;
$>
$> $$
-> language sql immutable;
CREATE FUNCTION
*> select * from explode_array(array[1, 3]);
explode_array
---------------
1
3
(2 rows)

*> select avg(*) from explode_array(array[1, 3]);
avg
------------------------
1.00000000000000000000
(1 row)

*> select sum(*) from explode_array(array[1, 3]);
sum
-----
2
(1 row)

I would have expected an avg of 2.0 and a sum of 4, where am I going wrong?
Or is there a better way to get the avg of 2 dates (median would
suffice, but I don't know the interval length in days before hand - and
thus not the middle point of the interval).

--
Alban Hertroys
alban(at)magproductions(dot)nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede

// Integrate Your World //

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Lanzarotta 2007-09-11 15:50:47 Question about a query with two count fields
Previous Message George Pavlov 2007-09-11 15:50:02 creating/dropping tables inside functions?