X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.225.181]) by svr1.postgresql.org (Postfix) with ESMTP id 655AACC17D for ; Tue, 15 Jul 2003 12:39:10 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr5.postgresql.org [64.117.225.181]) (amavisd-new, port 10024) with ESMTP id 17639-03 for ; Tue, 15 Jul 2003 12:39:23 +0000 (GMT) Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.185]) by svr1.postgresql.org (Postfix) with ESMTP id 7BA6BCBF0B for ; Tue, 15 Jul 2003 09:38:58 -0300 (ADT) Received: from [212.227.126.161] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 19cP5a-0002a4-00 for pgsql-sql@postgresql.org; Tue, 15 Jul 2003 14:39:26 +0200 Received: from [80.131.204.217] (helo=goofy.thiele-intern) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 19cP5a-00029U-00 for pgsql-sql@postgresql.org; Tue, 15 Jul 2003 14:39:26 +0200 Date: Tue, 15 Jul 2003 14:39:25 +0200 From: Erik Thiele To: pgsql-sql@postgresql.org Subject: Re: summing tables Message-Id: <20030715143925.54716ed4.erik@thiele-hydraulik.de> In-Reply-To: <001501c34aca$e70ca280$0600a8c0@fix.ro> References: <20030715140921.7c5a6a6c.erik@thiele-hydraulik.de> <001501c34aca$e70ca280$0600a8c0@fix.ro> Organization: Thiele-Hydraulik X-Mailer: Sylpheed version 0.8.10 (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200307/164 X-Sequence-Number: 14278 On Tue, 15 Jul 2003 15:16:21 +0300 "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. look, the c value is set by one row-query and read by the row-query of the row below. does sql specify some "order is magically always as you expect it" rule? still i am a little confused. and i am sorry, i didn't initially specify that the "seq" are not gapless. i.e. seq-1 does not always exist. but seq-13 could be the next lower one! zeit=# select * from foo; seq | a | b | c -----+----+----+--- 0 | 1 | 2 | 3 1 | 1 | 2 | 2 | 5 | 7 | 3 | -2 | -4 | 6 | -1 | -2 | 5 | -2 | -2 | 4 | 0 | 1 | (7 rows) i created this (gapless for easiness) table and run your query: zeit=# update foo set c = a + b + (select c from foo as x where x.seq = seq-1) where c is null; UPDATE 6 #### 6 updates??? really??? zeit=# select * from foo; seq | a | b | c -----+----+----+--- 0 | 1 | 2 | 3 1 | 1 | 2 | 2 | 5 | 7 | 3 | -2 | -4 | 6 | -1 | -2 | 5 | -2 | -2 | 4 | 0 | 1 | (7 rows) hmmmm. let's try the statement of the other reply to my initial mail: UPDATE foo t set c = a + b + (Select c from foo tt where tt.seq = t.seq -1) zeit=# UPDATE foo t set c = a + b + (Select c from foo tt where tt.seq = t.seq -1); ERROR: parser: parse error at or near "t" hmmmm.... any clues? cu & thx erik > additional checks are required if you want to update c when c is not null > if all the c are null then this query will do nothing > > i have a table consisting of 4 integers. > > > > seq is for making the table ordered. (ORDER BY SEQ ASC) > > a,b,c maybe null > > > > > > seq | a | b | c > > -----+----+----+--- > > 0 | 1 | 2 | 3 > > 1 | 1 | 2 | > > 2 | 5 | 7 | > > 3 | -2 | -4 | > > > > > > i am needing a sql statement to do > > > > c=a+b+"the c of the row with seq one less than myself" > > > > this statement has to run over the whole table, in seq order. -- Erik Thiele