Re: summing tables - Mailing list pgsql-sql

From Greg Stark
Subject Re: summing tables
Date
Msg-id 878yqzn8k5.fsf@stark.dyndns.tv
Whole thread Raw
In response to Re: summing tables  ("Viorel Dragomir" <bigchief@vio.ro>)
List 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



pgsql-sql by date:

Previous
From: Christoph Haller
Date:
Subject: Re: Non-Blocking Locks (i.e. Oracle NOWAIT)
Next
From: Dmitry Tkach
Date:
Subject: Re: Count dates distinct within an interval