From: | Greg Stark <gsstark(at)mit(dot)edu> |
---|---|
To: | "Viorel Dragomir" <bigchief(at)vio(dot)ro> |
Cc: | <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: summing tables |
Date: | 2003-07-15 14:55:38 |
Message-ID: | 878yqzn8k5.fsf@stark.dyndns.tv |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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
From | Date | Subject | |
---|---|---|---|
Next Message | Dmitry Tkach | 2003-07-15 15:00:19 | Re: Count dates distinct within an interval |
Previous Message | Christoph Haller | 2003-07-15 14:54:48 | Re: Non-Blocking Locks (i.e. Oracle NOWAIT) |