Re: aggregate function for median calculation

From: Philip Hallstrom <philip(at)adhesivemedia(dot)com>
To: "Thalis A(dot) Kalfigopoulos" <thalis(at)cs(dot)pitt(dot)edu>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: aggregate function for median calculation
Date: 2001-06-18 23:31:28
Message-ID: 20010618162930.E9218-100000@oddjob.adhesivemedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I missed the first part, but if the numbers are rows in a table, why not
do something like:

numrows = select count(*) from table1 where some_condition
median_value = select some_col from table1 where some_condition order by
some_col limit numrows/2, 1

(or something very close to that anyway).

-philip

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
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://www.postgresql.org/search.mpl
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joseph Shraibman 2001-06-19 00:50:49 max_expr_depth
Previous Message Adam Haberlach 2001-06-18 23:25:46 Web site gripes