Postgresql Percentile_Cont 不工作:函数 percentile_cont(数字,字符变化,数字)不存在

Postgresql Percentile_Cont not working: function percentile_cont(numeric, character varying, numeric) does not exist

我有一个 table TableX 看起来像:

Name Value
Bob 100
Cathy 500
Cathy 200
Cathy 100

我想处理值列的中位数以获得:

Name Value
Bob 100
Cathy 200

我正在尝试使用 percentile_cont:

select name, percentile_cont(0.5) within group (order by name, value) from TableX

但是获取错误:

ERROR:  function percentile_cont(numeric, character varying, numeric) does not exist

我的 postgresql 版本是 13+,下面一行确实有效。

SELECT percentile_cont(array(SELECT j/10.0 FROM generate_series(1,10) j)) WITHIN GROUP(ORDER BY i) AS median FROM generate_series(1,11) AS s(i);

很确定我的脚本是错误的...非常感谢您的想法。谢谢!

create table percent_test (name varchar, value float);

insert into percent_test values ('Bob', 100), ('Cathy', 500), ('Cathy', 200), ('Cathy', 100);

select name, percentile_cont(0.5) within group (order by value) from  percent_test group  by name;
 name  | percentile_cont 
-------+-----------------
 Bob   |             100
 Cathy |             200

根据 Aggregate Functions:

percentile_cont ( fraction double precision ) WITHIN GROUP ( ORDER BY double precision ) → double precision

percentile_cont ( fraction double precision ) WITHIN GROUP ( ORDER BY interval ) → interval

Computes the continuous percentile, a value corresponding to the specified fraction within the ordered set of aggregated argument values. This will interpolate between adjacent input items if needed.

所以within group中的order by只能是数值,不能是字符值。将 name 分组推送到查询的 from 部分。