From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | Chandru Aroor <caroor(at)yahoo(dot)com> |
Cc: | "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org> |
Subject: | Re: Calculating Median value |
Date: | 2018-05-06 15:43:58 |
Message-ID: | 9087.1525621438@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Chandru Aroor <caroor(at)yahoo(dot)com> writes:
> ... median is defines as follows:
> CREATE AGGREGATE public.median(anyelement) ( SFUNC=array_append, STYPE=anyarray, FINALFUNC=array_median
> I will be honest. I have no clue how that works or what it is supposed
> to do.
It's a user-defined aggregate. See
https://www.postgresql.org/docs/current/static/xaggr.html
array_append is a built-in function that, in this usage, would just serve
to collect all the input values into an array. array_median is not the
name of any built-in function, so it must be user-written code. What
I'd expect it to do, if it's trying to implement the usual definition of
median, is to sort the array and then take the middle element. Judging
from your description, it's not doing that.
If you're using PG 9.4 or later, you could skip trying to debug this
homegrown version of median and instead use the standard ordered-set
aggregate percentile_cont, or possibly percentile_disc. See
https://www.postgresql.org/docs/current/static/sql-expressions.html#SYNTAX-AGGREGATES
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | Chandru Aroor | 2018-05-06 17:06:23 | Re: Calculating Median value |
Previous Message | Chandru Aroor | 2018-05-06 13:19:31 | Calculating Median value |