Re: summing tables

From: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
To: Erik Thiele <erik(at)thiele-hydraulik(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: summing tables
Date: 2003-07-15 15:02:17
Message-ID: 3F141779.204E02CD@nsd.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


Erik,

If you intent is to get a running total of a and b ordered by seq, you
should try this (assuming the table name is t):

update t set c = ( select sum(a) + sum(b) from t t1 where t1.seq <=
t.seq);

You should have an index on seq.
If the table is very large, it is going to be painfully slow.
In that case you may want to think about using a function to step thru
each row.

JLL

Erik Thiele wrote:
>
> hi,
>
> 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.
>
> how can this be acomplished???
>
> cu&thanks
> erik
>
> --
> Erik Thiele
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Viorel Dragomir 2003-07-15 15:06:42 Re: summing tables
Previous Message Dmitry Tkach 2003-07-15 15:00:19 Re: Count dates distinct within an interval