假设我有以下表定义:
CREATE TABLE x (i serial primary key, value integer not null);
我想计算value
(不是AVG)的MEDIAN。中位数是在包含相同数量元素的两个子集中划分集合的值。如果元素的数量是偶数,则中位数是最低段中的最大值和最大段的最低值的平均值。 (有关详细信息,请参阅维基百科。)
以下是我设法计算MEDIAN的方法,但我想必须有更好的方法:
SELECT AVG(values_around_median) AS median
FROM (
SELECT
DISTINCT(CASE WHEN FIRST_VALUE(above) OVER w2 THEN MIN(value) OVER w3 ELSE MAX(value) OVER w2 END)
AS values_around_median
FROM (
SELECT LAST_VALUE(value) OVER w AS value,
SUM(COUNT(*)) OVER w > (SELECT count(*)/2 FROM x) AS above
FROM x
GROUP BY value
WINDOW w AS (ORDER BY value)
ORDER BY value
) AS find_if_values_are_above_or_below_median
WINDOW w2 AS (PARTITION BY above ORDER BY value DESC),
w3 AS (PARTITION BY above ORDER BY value ASC)
) AS find_values_around_median
有什么想法吗?
答案 0 :(得分:23)
是的,使用PostgreSQL 9.4,您可以使用新引入的逆分布函数PERCENTILE_CONT()
,这是一个在SQL标准中指定的有序集合函数。
WITH t(value) AS (
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 100
)
SELECT
percentile_cont(0.5) WITHIN GROUP (ORDER BY value)
FROM
t;
This emulation of MEDIAN()
via PERCENTILE_CONT()
is also documented here
答案 1 :(得分:15)
确实有一种更简单的方法。在Postgres中,您可以定义自己的聚合函数。我发布了一些函数,用于对PostgreSQL片段库进行中位数以及模式和范围。
答案 2 :(得分:7)
更简单的查询:
WITH y AS (
SELECT value, row_number() OVER (ORDER BY value) AS rn
FROM x
WHERE value IS NOT NULL
)
, c AS (SELECT count(*) AS ct FROM y)
SELECT CASE WHEN c.ct%2 = 0 THEN
round((SELECT avg(value) FROM y WHERE y.rn IN (c.ct/2, c.ct/2+1)), 3)
ELSE
(SELECT value FROM y WHERE y.rn = (c.ct+1)/2)
END AS median
FROM c;
avg()
。结果为数字,四舍五入到小数点后3位。测试表明,新版本比问题中的查询快4倍(并产生正确的结果):
CREATE TEMP TABLE x (value int);
INSERT INTO x SELECT generate_series(1,10000);
INSERT INTO x VALUES (NULL),(NULL),(NULL),(3);
答案 3 :(得分:0)
对于googlers:还有http://pgxn.org/dist/quantile 安装此扩展程序后,可以在一行中计算中位数。
答案 4 :(得分:0)
仅具有原生postgres函数的简单sql:
select
case count(*)%2
when 1 then (array_agg(num order by num))[count(*)/2+1]
else ((array_agg(num order by num))[count(*)/2]::double precision + (array_agg(num order by num))[count(*)/2+1])/2
end as median
from unnest(array[5,17,83,27,28]) num;
当然,如果要处理空值,可以添加coalesce()或其他东西。
答案 5 :(得分:0)
CREATE TABLE array_table (id integer, values integer[]) ;
INSERT INTO array_table VALUES ( 1,'{1,2,3}');
INSERT INTO array_table VALUES ( 2,'{4,5,6,7}');
select id, values, cardinality(values) as array_length,
(case when cardinality(values)%2=0 and cardinality(values)>1 then (values[(cardinality(values)/2)]+ values[((cardinality(values)/2)+1)])/2::float
else values[(cardinality(values)+1)/2]::float end) as median
from array_table
或者您可以创建一个函数并在进一步查询的任何位置使用它。
CREATE OR REPLACE FUNCTION median (a integer[])
RETURNS float AS $median$
Declare
abc float;
BEGIN
SELECT (case when cardinality(a)%2=0 and cardinality(a)>1 then
(a[(cardinality(a)/2)] + a[((cardinality(a)/2)+1)])/2::float
else a[(cardinality(a)+1)/2]::float end) into abc;
RETURN abc;
END;
$median$
LANGUAGE plpgsql;
select id,values,median(values) from array_table
答案 6 :(得分:0)
使用Below函数查找第n个百分位数
CREATE or REPLACE FUNCTION nth_percentil(anyarray, int)
RETURNS
anyelement as
$$
SELECT $1[$2/100.0 * array_upper($1,1) + 1] ;
$$
LANGUAGE SQL IMMUTABLE STRICT;
在你的情况下,它是第50百分位。
使用以下查询获取中位数
SELECT nth_percentil(ARRAY (SELECT Field_name FROM table_name ORDER BY 1),50)
这将给你50%的百分位,这基本上是中位数。
希望这有用。