From: | "Thomas Keller" <kellert(at)ohsu(dot)edu> |
---|---|
To: | "Devin Ben-Hur" <devin(at)ben-hur(dot)net> |
Cc: | "Postgresql PDX_Users" <pdxpug(at)postgresql(dot)org> |
Subject: | Re: sum of array elements |
Date: | 2008-10-24 16:36:20 |
Message-ID: | 9025C6A8-55A4-432F-9CB6-AB2C67B1F6A2@ohsu.edu |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pdxpug |
Thanks Devin.
I got an error when I gave ( an_array ) as the input type. So I
changed that to ( DECIMAL [] ) and it worked great.
thanks so much.
Tom
kellert(at)ohsu(dot)edu
503-494-2442
On Oct 23, 2008, at 10:04 PM, Devin Ben-Hur wrote:
> 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(
> array_lower($1,1),
> array_upper($1,1)
> ) 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
>
>
From | Date | Subject | |
---|---|---|---|
Next Message | David E. Wheeler | 2008-10-24 16:48:16 | Re: sum of array elements |
Previous Message | Devin Ben-Hur | 2008-10-24 05:04:52 | Re: sum of array elements |