pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Erik Thiele <erik@thiele-hydraulik.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: summing tables
Date: Tue, 15 Jul 2003 11:14:56 -0400
Message-ID: <22671.1058282096@sss.pgh.pa.us> (raw)
In-Reply-To: <20030715143925.54716ed4.erik@thiele-hydraulik.de>
References: <20030715140921.7c5a6a6c.erik@thiele-hydraulik.de>
	<001501c34aca$e70ca280$0600a8c0@fix.ro>
	<20030715143925.54716ed4.erik@thiele-hydraulik.de>

Erik Thiele <erik@thiele-hydraulik.de> writes:
> "Viorel Dragomir" <bigchief@vio.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



view thread (13+ messages)  latest in thread

Message-ID: <22671.1058282096@sss.pgh.pa.us>
Permalink:  ../22671.1058282096@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/22671.1058282096@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, erik@thiele-hydraulik.de
  Subject: Re: summing tables
  In-Reply-To: <22671.1058282096@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox