From: | Chandru Aroor <caroor(at)yahoo(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
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 17:06:23 |
Message-ID: | 1526435930.410638.1525626383262@mail.yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-novice |
Thanks Tom. You are actually correct. In talking with the business user, we need to get the 50th percentile, and picking the higher value for even number of members, so median as Excel calculates it is actually not appropriate. Will aggregate percentile_cont, or possibly percentile_disc do it?
On Sunday, May 6, 2018, 10:44:19 AM CDT, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
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 | Tom Lane | 2018-05-06 18:13:47 | Re: Calculating Median value |
Previous Message | Tom Lane | 2018-05-06 15:43:58 | Re: Calculating Median value |