X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id F0BD73A55B9 for ; Mon, 24 Jan 2005 17:45:09 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 73205-06 for ; Mon, 24 Jan 2005 17:44:59 +0000 (GMT) Received: from lucifer.oficina (unknown [200.68.121.201]) by svr1.postgresql.org (Postfix) with ESMTP id 062B03A55C4 for ; Mon, 24 Jan 2005 17:44:54 +0000 (GMT) Received: from localhost (localhost.akyasociados.com.ar [127.0.0.1]) by lucifer.oficina (8.13.1/8.12.10) with ESMTP id j0OHipwL006593; Mon, 24 Jan 2005 14:44:51 -0300 (ART) (envelope-from franco@akyasociados.com.ar) Received: from lucifer.oficina ([127.0.0.1]) by localhost (lucifer.oficina [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 06348-02; Mon, 24 Jan 2005 14:44:48 -0300 (ART) Received: from [192.168.1.102] (malevolgia.oficina [192.168.1.102]) (authenticated bits=0) by lucifer.oficina (8.13.1/8.12.10) with ESMTP id j0OHikAg006590; Mon, 24 Jan 2005 14:44:47 -0300 (ART) (envelope-from franco@akyasociados.com.ar) Message-ID: <41F5340D.8020601@akyasociados.com.ar> Date: Mon, 24 Jan 2005 14:44:45 -0300 From: franco User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: mrblonde@locked.myftp.org Cc: pgsql-sql@postgresql.org Subject: Re: update from multiple rows References: In-Reply-To: Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at akyasociados.com.ar X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.123 tagged_above=0 required=5 tests=AWL, RCVD_IN_SORBS_DUL X-Spam-Level: X-Archive-Number: 200501/212 X-Sequence-Number: 20000 I understand data_sys is the average value for the 3 days, from at the day before to the day after. This should do what you want, in one pass. Check the average function in the subselect. If what you want is to divide by 3 no matter how many records where found, enable the commented line. UPDATE mytable SET data_sys=TMP.average FROM ( --get the averages by date SELECT MT1.date AS date, avg(MT2.data_raw) AS average --sum(MT2.data_raw)/3 AS average FROM mytable MT1 INNER JOIN mytable MT2 ON (MT2.date BETWEEN MT1.date-1 AND MT1.date+1) GROUP BY MT1.date ) AS TMP WHERE mytable.date=TMP.date Hope this is what you where looking for. adam etienne wrote: > hi > I have some trouble updating a table like this one : > date | data_raw | data_sys > 12-01 | 5 | 4.5 > 13-01 | 6 | 6 > 14-01 | 7 | 8 > > I would like to update the 'data_sys' row by computing values of > multiple 'data_raw' values. I mean for example : > data_sys(13-01) = (data_raw['12-01'] + data_raw['13-01'] + > data_raw['14-01'] )/3; > > I thought of a function that fetch the 3 data_raw rows for each > rows.... but it was obviously too much slow... > > Is there a more efficient way to achieve this ? > Thanks in advance.. This could help me very much.. > > Etienne Adam > > _________________________________________________________________ > 無料メールならやっぱり 「MSN Hotmail」 http://www.hotmail.com/ > > ---------------------------(end of broadcast)--------------------------- > TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org >