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" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, July 15, 2003 3:39 PM
Subject: Re: [SQL] summing tables


> On Tue, 15 Jul 2003 15:16:21 +0300
> "Viorel Dragomir" <[EMAIL PROTECTED]> 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 [EMAIL PROTECTED] so that your
>       message can get through to the mailing list cleanly


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [EMAIL PROTECTED] so that your
      message can get through to the mailing list cleanly

Reply via email to