Re: summing tables

From: "Viorel Dragomir" <bigchief(at)vio(dot)ro>
To: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: summing tables
Date: 2003-07-15 12:16:21
Message-ID: 001501c34aca$e70ca280$0600a8c0@fix.ro
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

update table_name
set c = a + b + (select c from table_name as x where x.seq = seq-1)
where c is null;

additional checks are required if you want to update c when c is not null
if all the c are null then this query will do nothing

god help u
:)

----- Original Message -----
From: "Erik Thiele" <erik(at)thiele-hydraulik(dot)de>
To: <pgsql-sql(at)postgresql(dot)org>
Sent: Tuesday, July 15, 2003 3:09 PM
Subject: [SQL] summing tables

> 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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Dani Oderbolz 2003-07-15 12:17:50 Re: summing tables
Previous Message Erik Thiele 2003-07-15 12:09:21 summing tables