pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jean-Luc Lachance <jllachan@nsd.ca>
To: Erik Thiele <erik@thiele-hydraulik.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: summing tables
Date: Tue, 15 Jul 2003 11:02:17 -0400
Message-ID: <3F141779.204E02CD@nsd.ca> (raw)
References: <20030715140921.7c5a6a6c.erik@thiele-hydraulik.de>


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@postgresql.org



view thread (13+ messages)  latest in thread

Message-ID: <3F141779.204E02CD@nsd.ca>
Permalink:  ../3F141779.204E02CD@nsd.ca/
Also on:    postgresql.org/message-id/3F141779.204E02CD@nsd.ca

 · 

reply

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: jllachan@nsd.ca, erik@thiele-hydraulik.de
  Subject: Re: summing tables
  In-Reply-To: <3F141779.204E02CD@nsd.ca>

* 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