Re: summing tables

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Erik Thiele <erik(at)thiele-hydraulik(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: summing tables
Date: 2003-07-15 15:14:56
Message-ID: 22671.1058282096@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Erik Thiele <erik(at)thiele-hydraulik(dot)de> writes:
> "Viorel Dragomir" <bigchief(at)vio(dot)ro> 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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Dmitry Tkach 2003-07-15 15:16:33 Re: Non-Blocking Locks (i.e. Oracle NOWAIT)
Previous Message Viorel Dragomir 2003-07-15 15:06:42 Re: summing tables