From: Erik Thiele <erik@thiele-hydraulik.de>
To: pgsql-sql@postgresql.org
Subject: Re: summing tables
Date: Tue, 15 Jul 2003 14:39:25 +0200
Message-ID: <20030715143925.54716ed4.erik@thiele-hydraulik.de> (raw)
In-Reply-To: <001501c34aca$e70ca280$0600a8c0@fix.ro>
References: <20030715140921.7c5a6a6c.erik@thiele-hydraulik.de>
<001501c34aca$e70ca280$0600a8c0@fix.ro>
On Tue, 15 Jul 2003 15:16:21 +0300
"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.
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
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: erik@thiele-hydraulik.de
Subject: Re: summing tables
In-Reply-To: <20030715143925.54716ed4.erik@thiele-hydraulik.de>
* 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