From: | "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | "Scara Maccai" <m_lists(at)yahoo(dot)it> |
Cc: | pgsql-general <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Array operator "sum array values" |
Date: | 2008-03-31 15:29:25 |
Message-ID: | 162867790803310829q7990facdo4c02fd56a71c7b7d@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Hello
On 31/03/2008, Scara Maccai <m_lists(at)yahoo(dot)it> wrote:
> Hi,
>
> is there a syntax that sums the values of an array?
>
> That is, having an array like
>
> {1,3,8},{5,6,7}
>
> something like
>
> select arr[1:2][2:3]
>
> gives
>
> {1,3},{6,7}
>
>
> ; what I'd like is:
>
> select arr[1$2][2$3]
>
> gives:
>
> 17 (that is, 1+3 + 6+7)
>
> If there is no such operator, would it be complicated adding it to postgres?
> I mean: is the "[:]" operator an internal thing (the parser "knows" about it) or is it just like any other operators that can be added ?
>
>
create or replace function sum_elements(anyarray)
returns anyelement as $$
select sum($1[i])
from generate_series(array_lower($1,1),
array_upper($1,1)) g(i);
$$ language sql immutable;
select sum_elements(array[1,2,3]); --> 6
Regards
Pavel Stehule
>
>
>
>
>
>
>
> ___________________________________
> Scopri il Blog di Yahoo! Mail: trucchi, novità, consigli... e la tua opinione!
> http://www.ymailblogit.com/blog/
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2008-03-31 15:32:43 | Re: [HACKERS] ANALYZE getting dead tuple count hopelessly wrong |
Previous Message | Scara Maccai | 2008-03-31 15:08:37 | Array operator "sum array values" |