From: | elein <elein(at)varlena(dot)com> |
---|---|
To: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Running tally |
Date: | 2003-10-11 19:49:00 |
Message-ID: | 20031011124900.G6483@cookie.varlena.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
You can use plpythonu (or tcl or C or R) to do running
sums. For plpythonu, you must initialize the SD[]
by calling it first with the proper argument.
create or replace function runsum(int,int)
returns int as
'
if args[0] == 1:
SD["currval"] = 0
return SD["currval"]
else:
try:
SD["currval"] += args[1]
except:
SD["currval"] = args[1]
return SD["currval"]
' language 'plpython';
select runsum(1,0);
select num, runsum(0,num) from tallytable;
Variations on this technique are discussed on
General Bits http://www.varlena.com/GeneralBits
under the Tidbits area listing talks from OSCON2003.
elein(at)varlena(dot)com
webstat=# select runsum(0,code), code, doc from temp_rawlogs;
n Sat, Oct 04, 2003 at 05:56:38PM +0800, Christopher Kings-Lynne wrote:
> Hi guys,
>
> If I have a table that is just a single column full of numbers, how can
> I select all the rows from the table with a second column that is the
> running tally so far down the result set?
>
> eg:
>
> Num Tally so far
> 0.3 0.3
> 1.2 1.5
> 2.0 3.5
> ...
>
> Does this require PL/PgSQL coding? If so, how do you actually construct
> an arbitrary row for returning? The docs are somewhat unclear on this.
>
> Chris
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
From | Date | Subject | |
---|---|---|---|
Next Message | Roberto Mello | 2003-10-11 23:47:46 | Re: PL/PGSQL TUTORIAL |
Previous Message | Richard Huxton | 2003-10-11 18:43:38 | Re: Running tally |