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 60216CC323 for ; Tue, 15 Jul 2003 13:37:51 +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 73686-08 for ; Tue, 15 Jul 2003 13:38:14 +0000 (GMT) Received: from mail1.dr.myx.net (mail1.dr.myx.net [217.10.193.161]) by svr1.postgresql.org (Postfix) with ESMTP id 6CAC2CB9AA for ; Tue, 15 Jul 2003 10:37:49 -0300 (ADT) Received: from workstation24 (unknown [81.12.137.146]) by mail1.dr.myx.net (mydomain.myx.net) with ESMTP id DBC7630075B for ; Tue, 15 Jul 2003 16:38:04 +0300 (EEST) Message-ID: <007101c34ad6$3e0249e0$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> Subject: Re: summing tables Date: Tue, 15 Jul 2003 16:37:31 +0300 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" 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/168 X-Sequence-Number: 14282 Ok. I disregarded the complexity of this problem. :) You may wish to do this thing from a programming language with every row at a time [in php, asp...]. Anyway here is a function in plpgsql. It solves your problem, i hope; but i don't recommend it. create function update_nulls() returns int as ' declare var1 integer; var2 integer; begin select into var1 count(*) from tab where c is null; var2 := var1; while var1 > 0 loop update table_name set c = a + b + (select x.c from table_name as x where x.seq = table_name.seq-1) where c is null; var1 := var1 - 1; end loop; return var2; end; ' language 'plpgsql'; ----- Original Message ----- From: "Erik Thiele" To: Sent: Tuesday, July 15, 2003 3:39 PM Subject: Re: [SQL] summing tables > 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 > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly