Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 540ED634371 for ; Mon, 18 May 2009 10:32:02 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14692-05 for ; Mon, 18 May 2009 10:32:00 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-fx0-f222.google.com (mail-fx0-f222.google.com [209.85.220.222]) by mail.postgresql.org (Postfix) with ESMTP id 62562634CAC for ; Mon, 18 May 2009 10:32:00 -0300 (ADT) Received: by fxm22 with SMTP id 22so3537175fxm.19 for ; Mon, 18 May 2009 06:31:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=0ZXgzNYZZVkl4bgTOdq+lKQ9auQwozWlTtggmQettYo=; b=XJ4pu7sKKsVB4qUu7Ps/93EsFIfkKeHAn4MhQXfU56NPOJX9+U0LsFBqZD6xT1YTKB t2WG+glQimqeHIJM2WVB/o7GvI04rZM72nP7tHlXcvWuO308Z+wUr5SnZl978J6KaI2+ spYEyG0OVuQ9uVO0wvERqV5a7COFtHWL50up8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=TZDB29F3bNhHoUXiVZvkmI/Y+UbRO66+xGusQfr1Y6OlSqUkAMeFW6s1GD/4v0z+bd H73EAYRhSBmwFPIFE+rUoMw0V+F4TA/A6vo40ItJLbDTEC67IAYyI+E/A8unRIbchzhO IZgNkDOVrBc88/NgJKr4bN30cQ4jQ6jpE8heg= MIME-Version: 1.0 Received: by 10.223.127.8 with SMTP id e8mr4325449fas.81.1242653518189; Mon, 18 May 2009 06:31:58 -0700 (PDT) In-Reply-To: <003f01c9d7a6$4a6d9b90$df48d2b0$@es> References: <003f01c9d7a6$4a6d9b90$df48d2b0$@es> Date: Mon, 18 May 2009 15:31:57 +0200 Message-ID: Subject: Re: SUM Array values query From: Pawel Socha To: =?ISO-8859-1?Q?Dani_Casta=F1os?= Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/80 X-Sequence-Number: 32572 2009/5/18 Dani Casta=F1os : > Hi all, > > I've this query including arrays: > > SELECT hour[1:5], statistics_date > =A0FROM statistics_daily > =A0WHERE statistics_date =3D to_date( '03/01/2008', 'DD/MM/YYYY' ) > > Result: > > =A0 =A0 =A0 =A0 =A0 =A0hour =A0 =A0 =A0 =A0 =A0 =A0| statistics_date > ----------------------------+----------------- > =A0{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: > > =A0 =A0 =A0 =A0 =A0 =A0hour =A0 =A0 =A0 =A0 =A0 =A0| statistics_date > ----------------------------+----------------- > =A09000 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | 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=F1os S=E1nchez > 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=3D `-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=3D `-merlin>select array_sum(col_array) from t30; array_sum ----------- 9000 (1 row) :) --=20 Pawel Socha pawel.socha@gmail.com