Re: (P)SQL for a sum with constraints

From: hubert depesz lubaczewski <depesz(at)depesz(dot)com>
To: Shug Boabby <shug(dot)boabby(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: (P)SQL for a sum with constraints
Date: 2009-04-15 10:47:09
Message-ID: 20090415104709.GA2704@depesz.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Apr 15, 2009 at 11:09:49AM +0100, Shug Boabby wrote:
> Anyone have any ideas how to do this? I'm able to do it
> programmatically, but it's slow. Optimally I'd like to be able to do
> this in the DB. As you can see, it's a little trickier that the usual
> aggregate function with a GROUP BY and HAVING.

this is called cumulative sum, and can be done quite easily.

check this blogpost (but also its comments! - the c function is not
necessary!)
http://www.depesz.com/index.php/2007/08/17/rownum-anyone-cumulative-sum-in-one-query/

depesz

--
Linkedin: http://www.linkedin.com/in/depesz / blog: http://www.depesz.com/
jid/gtalk: depesz(at)depesz(dot)com / aim:depeszhdl / skype:depesz_hdl / gg:6749007

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ow Mun Heng 2009-04-15 10:51:52 pg_reorg -> Anyone has any experience with it?
Previous Message Shug Boabby 2009-04-15 10:09:49 (P)SQL for a sum with constraints