Re: complicated running aggregate

From: Greg Smith <greg(at)2ndquadrant(dot)com>
To: Sim Zacks <sim(at)compulab(dot)co(dot)il>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: complicated running aggregate
Date: 2010-06-24 14:23:21
Message-ID: 4C236A59.1050504@2ndquadrant.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sim Zacks wrote:
> I am trying to build a query with a running total, but it is a bit
> complicated...Currently I'm generating this information using a plpsql loop, but I
> would like to change it to an SQL query, if possible.
>

It's hard to do a running total with the sort of requirements you have
using simple SQL. If you have PostgreSQL 8.4, it's much much easier to
do that using the SQL Window functions:
http://www.postgresql.org/docs/current/static/tutorial-window.html

If you're on an earlier version, using a loop in a function is not a bad
approach. Getting the query right without windowing is possible, but
painful enough that it may not be worth your trouble.

--
Greg Smith 2ndQuadrant US Baltimore, MD
PostgreSQL Training, Services and Support
greg(at)2ndQuadrant(dot)com www.2ndQuadrant.us

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2010-06-24 14:29:20 Re: unexpected effect of FOREIGN KEY ON CASCADE DELETE
Previous Message Grzegorz Jaśkiewicz 2010-06-24 14:21:04 Re: unexpected effect of FOREIGN KEY ON CASCADE DELETE