Skip site navigation (1) Skip section navigation (2)

Re: Sum

From: Marcelo Pereira <gandalf(at)sum(dot)desktop(dot)com(dot)br>
To: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Sum
Date: 2002-03-28 17:45:56
Message-ID: Pine.LNX.4.20.0203281445140.13932-100000@ni.hmmg.sp.gov.br (view raw or flat)
Thread:
Lists: pgsql-general
P E R F E C T !!!

Hello Jean-Luc,

Thanks for your answer, it was exacty what I was needing.

Best Regards,

See ya,

Marcelo Pereira

-- Remember that only God and Esc+:w saves.
        __
       (_.\           Marcelo Pereira       |
        / / ___                             |
       / (_/ _ \__    Matematica/99 - IMECC |
_______\____/_\___)___Unicamp_______________/

--- Jean-Luc Lachance, with his fast fingers, wrote:

:> 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
:> 


In response to

  • Re: Sum at 2002-03-28 17:40:10 from Jean-Luc Lachance

pgsql-general by date

Next:From: Andrew SullivanDate: 2002-03-28 18:04:16
Subject: Re: table design strategy
Previous:From: Jim MartinezDate: 2002-03-28 17:43:21
Subject: Re: Sum

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group