Re: aggregate function for median calculation

From: "Thalis A(dot) Kalfigopoulos" <thalis(at)cs(dot)pitt(dot)edu>
To: Alex Pilosov <alex(at)pilosoft(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: aggregate function for median calculation
Date: 2001-06-19 20:20:10
Message-ID: Pine.LNX.4.21.0106191617340.24987-100000@aluminum.cs.pitt.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, 18 Jun 2001, Alex Pilosov wrote:

> On Mon, 18 Jun 2001, Thalis A. Kalfigopoulos wrote:
>
> > Hippl,
> > I'm interested in calculating the median of a set of numbers.
> > The algorithm requires that all values are known in advance (ie stored
> > in an array). So the question is: how can I store everything first in
> > an array so I can later process it given that I'd like this to be an
> > aggregate function. I thought of creating an aggregate function and
> > have the state_function() gather all the values of a group in an array
> > and the final_function() to do the actuall median calculation on this
> > array. But the intermmediate state cannot hold multiple values in an
> > array (can it?) Any ideas on how to go with this?
>
> With current architecture, its kinda painful to implement such a function.
> Your 'state' function should allocate (palloc) memory for each element
> processed and then pfree it when you are done.
>
> -alex

So is there a way I can actually have a variable (the array with the elements) maintained between calls of the same function? I'm trying to figure how to design the aggregate's state_function that will do the element accumulation (before actually passing this array to the final to calculate the median).

TIA,
thalis

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2001-06-19 20:51:51 Re: aggregate function for median calculation
Previous Message Thalis A. Kalfigopoulos 2001-06-19 20:17:26 Re: aggregate function for median calculation