Re: SUM Array values query

From: Pawel Socha <pawel(dot)socha(at)gmail(dot)com>
To: Dani Castaños <dcastanos(at)androme(dot)es>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SUM Array values query
Date: 2009-05-18 13:31:57
Message-ID: cc4f12900905180631x26fbca2fwc4be6776b1a5eceb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

2009/5/18 Dani Castaños <dcastanos(at)androme(dot)es>:
> Hi all,
>
> I've this query including arrays:
>
> SELECT hour[1:5], statistics_date
>  FROM statistics_daily
>  WHERE statistics_date = to_date( '03/01/2008', 'DD/MM/YYYY' )
>
> Result:
>
>            hour            | statistics_date
> ----------------------------+-----------------
>  {1800,1800,1800,1800,1800} | 2008-01-03
>
>
> I'm just wondering if there's some way to retrieve the hour column as the
> sum of the array values... Just like this:
>
>            hour            | statistics_date
> ----------------------------+-----------------
>  9000                       | 2008-01-03
>
>
> Hour is a bigint[] array column.
> My version of postreSQL is 8.1.9
>
> Thank you in advanced!
>
> P.S.: Sorry if I had send it before, but I think I was not subscribed to the
> mailist.
>
> --
> Dani Castaños Sánchez
> dcastanos(at)androme(dot)es
>
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
Create function in pure sql and ...

,-[15:27:18]merlin(at)[local]:5432=
`-merlin>create function array_sum (bigint[]) returns bigint as $$
SELECT sum($1[i])::bigint FROM
generate_series(array_lower($1,1),array_upper($1,1)
) index(i); $$ language sql;
CREATE FUNCTION
Time: 16,203 ms
,-[15:28:02]merlin(at)[local]:5432=
`-merlin>select array_sum(col_array) from t30;
array_sum
-----------
9000
(1 row)

:)

--
Pawel Socha
pawel(dot)socha(at)gmail(dot)com

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Dani Castaños 2009-05-18 14:41:09 Extract week from date
Previous Message Dani Castaños 2009-05-18 13:25:44 Re: Extracting data from arrays