From: Pawel Socha <pawel.socha@gmail.com>
To: Dani Castaños <dcastanos@androme.es>
Cc: pgsql-sql@postgresql.org
Subject: Re: SUM Array values query
Date: Mon, 18 May 2009 15:31:57 +0200
Message-ID: <cc4f12900905180631x26fbca2fwc4be6776b1a5eceb@mail.gmail.com> (raw)
In-Reply-To: <003f01c9d7a6$4a6d9b90$df48d2b0$@es>
References: <003f01c9d7a6$4a6d9b90$df48d2b0$@es>
2009/5/18 Dani Castaños <dcastanos@androme.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@androme.es
>
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
Create function in pure sql and ...
,-[15:27:18]merlin@[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@[local]:5432=
`-merlin>select array_sum(col_array) from t30;
array_sum
-----------
9000
(1 row)
:)
--
Pawel Socha
pawel.socha@gmail.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: pawel.socha@gmail.com, dcastanos@androme.es
Subject: Re: SUM Array values query
In-Reply-To: <cc4f12900905180631x26fbca2fwc4be6776b1a5eceb@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox