Re: Re: [BUGS] BUG #4916: wish: more statistical functions (median, percentiles etc)

From: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
To: David Fetter <david(at)fetter(dot)org>
Cc: Richard Neill <rn214(at)cam(dot)ac(dot)uk>, PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Re: [BUGS] BUG #4916: wish: more statistical functions (median, percentiles etc)
Date: 2009-07-13 08:17:15
Message-ID: 2f4958ff0907130117k66578d01n89a16a011f49dfbf@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general

On Sun, Jul 12, 2009 at 5:53 PM, David Fetter<david(at)fetter(dot)org> wrote:
>> [mode would also be useful, as an explicit function, though we can get it
>> easily enough using count(1) order by count desc].
>
> You can get that with windowing functions, too. :)
>
>> According to google, this has been a wish since at least year 2000
>> for various people, but doesn't seem to be implemented.
>
> Patches are welcome :)

Trouble in writing such aggregate, would be that it has to keep full
set, in order to sort it, and choose n/2 element (in case set contains
odd number of elements), or ([n/2]+[(n/2)+1])/2 otherwise.

I usually, if in need to calculate it , I usually do it like that (but
that's pretty slow on large sets):

pg84(at)atlantic:~$ psql
psql (8.4beta2)
Type "help" for help.

pg84=# create table foo(a int not null);
CREATE TABLE
pg84=# insert into foo(a) select random()*666 from generate_series(1,666);
INSERT 0 666
pg84=# select a from foo order by a limit 1 offset (select count(*)/2 from foo);
a
-----
321
(1 row)

(yeah, I know it is lame).

So , I think in order to create such patch, the aggregate would have
to secretly create some temporary table, to store the set first...

--
GJ

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Frank Spies 2009-07-13 10:48:01 BUG #4918: Weird input syntax for intervals
Previous Message Tom Lane 2009-07-13 05:02:40 Re: BUG #4917: NULLs Last as a Global Option

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2009-07-13 08:31:17 Re: Best practices for moving UTF8 databases
Previous Message Albe Laurenz 2009-07-13 08:16:12 Re: Design question: Should "postgres" own all the db objects?