Skip site navigation (1) Skip section navigation (2)

Re: sum of array elements

From: Devin Ben-Hur <devin(at)ben-hur(dot)net>
To: Thomas Keller <kellert(at)ohsu(dot)edu>
Cc: Postgresql PDX_Users <pdxpug(at)postgresql(dot)org>
Subject: Re: sum of array elements
Date: 2008-10-24 05:04:52
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pdxpug
Thomas Keller wrote:
> Is there an easy way (succinct) to get the sum of values in an array. 
> E.g.
> a260_values is an array of decimal values. And I'd like to get the average.

create or replace function sum_decimal_array( an_array )
returns decimal
as $$
   select sum($1[i])
   from generate_series(
        ) g(i);
$$ language sql immutable;

select sum_decimal_array( ARRAY[ 1.1, 2.2, 3.3 ] ); -- yeilds 6.6

Devin Ben-Hur  503/860-4114  mailto:devin(at)ben-hur(dot)net

"Startups are basically comedies, or at least seem so in retrospect."
   -- Paul Graham

In response to


pdxpug by date

Next:From: Thomas KellerDate: 2008-10-24 16:36:20
Subject: Re: sum of array elements
Previous:From: Thomas KellerDate: 2008-10-23 18:23:02
Subject: sum of array elements

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group