X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.192]) by svr1.postgresql.org (Postfix) with ESMTP id 97AE7CC3EF for ; Tue, 15 Jul 2003 15:32:29 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr4.postgresql.org [64.117.224.192]) (amavisd-new, port 10024) with ESMTP id 19182-02 for ; Tue, 15 Jul 2003 15:32:54 +0000 (GMT) Received: from mail1.dr.myx.net (mail1.dr.myx.net [217.10.193.161]) by svr1.postgresql.org (Postfix) with ESMTP id 18E90CC3C6 for ; Tue, 15 Jul 2003 12:32:24 -0300 (ADT) Received: from workstation24 (unknown [81.12.137.146]) by mail1.dr.myx.net (mydomain.myx.net) with ESMTP id 4837E30067A for ; Tue, 15 Jul 2003 18:32:40 +0300 (EEST) Message-ID: <012b01c34ae6$40ab0870$0600a8c0@fix.ro> From: "Viorel Dragomir" To: References: <20030715140921.7c5a6a6c.erik@thiele-hydraulik.de> <001501c34aca$e70ca280$0600a8c0@fix.ro> <20030715143925.54716ed4.erik@thiele-hydraulik.de> <22671.1058282096@sss.pgh.pa.us> Subject: Re: summing tables Date: Tue, 15 Jul 2003 18:32:07 +0300 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-2" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/183 X-Sequence-Number: 14297 Indeed it was a mistake not to put the table_name. in where clause. But this doesn't resolve the problem. Do you know in which order the update will modify the rows? My lucky guess is that it takes from last inserted rows to the first row. In this way only one row gets updated correctly. I add the result from my unfortunate solution, even corrected. select * from table_name; seq | a | b | c -----+---+---+--- 1 | 1 | 2 | 3 2 | 5 | 9 | 3 | 1 | 2 | 4 | 4 | 7 | 5 | 4 | 2 | 6 | 0 | 1 | (6 rows) update table_name set c = a + b + (select c from table_name as x where seq = table_name.seq-1) where c is null; select * from table_name; seq | a | b | c -----+---+---+---- 1 | 1 | 2 | 3 2 | 5 | 9 | 17 3 | 1 | 2 | 4 | 4 | 7 | 5 | 4 | 2 | 6 | 0 | 1 | ----- Original Message ----- From: "Tom Lane" To: "Erik Thiele" Cc: Sent: Tuesday, July 15, 2003 6:14 PM Subject: Re: [SQL] summing tables > Erik Thiele writes: > > "Viorel Dragomir" wrote: > >> update table_name > >> set c = a + b + (select c from table_name as x where x.seq = seq-1) > >> where c is null; > > > hmmm. the query is run row by row, isn't it? > > but it will have different results depending on the order of those rows. > > No, it won't, because the SELECTs will not see the changes from the > not-yet-completed UPDATE. The above command is almost right; it needs > to be > > update table_name > set c = a + b + (select c from table_name as x where seq = table_name.seq-1) > where c is null; > > because inside the sub-SELECT, unadorned "seq" will refer to the SELECT's > table. > > You didn't say exactly what you wanted to do with null inputs, so that > issue may need more thought. > > regards, tom lane > > ---------------------------(end of broadcast)--------------------------- > TIP 5: Have you checked our extensive FAQ? > > http://www.postgresql.org/docs/faqs/FAQ.html