Re: Sum - Mailing list pgsql-general

From Jean-Luc Lachance
Subject Re: Sum
Date
Msg-id 3CA3557A.B6957EA6@nsd.ca
Whole thread Raw
In response to Sum  (Marcelo Pereira <gandalf@sum.desktop.com.br>)
Responses Re: Sum
Re: Sum
List pgsql-general
Marcelo,

Assuming that "cod" is unique and sequencial, you can use the following:

SELECT *, ( SELECT SUM( value) from table as t2 where t2.cod <= t1.cod)
as running_total
FROM table as t1
ORDER BY cod;

jll


Marcelo Pereira wrote:
>
> Hello All,
>
> I have a single table as:
>
> cod     date       value
> ---+-------------+-------
> 1  | 2002-03-12  |  5
> 2  | 2002-03-13  |  4
> 3  | 2002-03-13  |  7
> 4  | 2002-03-14  |  3
>
> ... and I would like to do a select that do `partial sums'.
>
> So, for instance, I would like to do a select that returns:
>
> cod     date       value   partial sum
> ---+-------------+-------+--------------
> 1  | 2002-03-12  |   5   |      5
> 2  | 2002-03-13  |   4   |      9
> 3  | 2002-03-13  |   7   |     16
> 4  | 2002-03-14  |   3   |     19
>
> As you can see, the row `partial sum' is what I am looking for. Do you
> have any idea how can I do it?
>
> Thanks in advance,
>
> Marcelo Pereira

pgsql-general by date:

Previous
From: Marcelo Pereira
Date:
Subject: Re: Sum
Next
From: Matthew Kirkwood
Date:
Subject: Re: Performance Tuning Document?