From: Greg Stark <gsstark@mit.edu>
To: Viorel Dragomir <bigchief@vio.ro>
Cc: pgsql-sql@postgresql.org
Subject: Re: summing tables
Date: 15 Jul 2003 10:55:38 -0400
Message-ID: <878yqzn8k5.fsf@stark.dyndns.tv> (raw)
In-Reply-To: <007101c34ad6$3e0249e0$0600a8c0@fix.ro>
References: <20030715140921.7c5a6a6c.erik@thiele-hydraulik.de>
<001501c34aca$e70ca280$0600a8c0@fix.ro>
<20030715143925.54716ed4.erik@thiele-hydraulik.de>
<007101c34ad6$3e0249e0$0600a8c0@fix.ro>
To solve this problem efficiently you probably need the lead/lag analytic
functions. Unfortunately Postgres doesn't have them.
You could do it with something like:
update foo set c = a+b+(select c from foo as x where seq < foo.seq ORDER BY seq desc LIMIT 1)
or the more standard but likely to be way slower:
update foo set c = a+b+(select c from foo as x where seq = (select max(seq) from foo as y where seq < foo.seq))
However, i would suggest that if you have an implicit relationship between
records you should make that relationship explicit with a foreign key. If you
had a column that contained the seq of the parent record then this would be
easy. I'm really puzzled how this query as currently specified could be
useful.
--
greg
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: gsstark@mit.edu, bigchief@vio.ro
Subject: Re: summing tables
In-Reply-To: <878yqzn8k5.fsf@stark.dyndns.tv>
* 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