PostgreSQL相当于Oracle的PERCENTILE_CONT函数

问题描述 投票:9回答:2

有没有人发现PostgreSQL相当于Oracle的PERCENTILE_CONT函数?我搜查了,找不到一个,所以我写了自己的。

这是我希望能帮到你的解决方案。

我工作的公司想要将Java EE Web应用程序从使用Oracle数据库迁移到使用PostgreSQL。几个存储过程严重依赖于使用Oracle独特的PERCENTILE_CONT()函数。 PostgreSQL中不存在此功能。

我试着查看是否有人将该功能“移植”到PG中无济于事。

oracle function postgresql percentile
2个回答
18
投票

经过更多搜索后,我找到了一个页面,列出了Oracle在以下方面实现此功能的伪代码:

http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions110.htm

我决定在PG中编写自己的函数来模仿Oracle的功能。

我找到了David Fetter的数组排序技术::

http://postgres.cz/wiki/PostgreSQL_SQL_Tricks#General_array_sort

Sorting array elements

这里(为了清楚起见)是David的代码:

CREATE OR REPLACE FUNCTION array_sort (ANYARRAY)
RETURNS ANYARRAY LANGUAGE SQL
AS $$
SELECT ARRAY(
    SELECT $1[s.i] AS "foo"
    FROM
        generate_series(array_lower($1,1), array_upper($1,1)) AS s(i)
    ORDER BY foo
);
$$;

所以这是我写的函数:

CREATE OR REPLACE FUNCTION percentile_cont(myarray real[], percentile real)
RETURNS real AS
$$

DECLARE
  ary_cnt INTEGER;
  row_num real;
  crn real;
  frn real;
  calc_result real;
  new_array real[];
BEGIN
  ary_cnt = array_length(myarray,1);
  row_num = 1 + ( percentile * ( ary_cnt - 1 ));
  new_array = array_sort(myarray);

  crn = ceiling(row_num);
  frn = floor(row_num);

  if crn = frn and frn = row_num then
    calc_result = new_array[row_num];
  else
    calc_result = (crn - row_num) * new_array[frn] 
            + (row_num - frn) * new_array[crn];
  end if;

  RETURN calc_result;
END;
$$
  LANGUAGE 'plpgsql' IMMUTABLE;

以下是一些比较测试的结果:

CREATE TABLE testdata
(
  intcolumn bigint,
  fltcolumn real
);

这是测试数据:

insert into testdata(intcolumn, fltcolumn)  values  (5, 5.1345);
insert into testdata(intcolumn, fltcolumn)  values  (195, 195.1345);
insert into testdata(intcolumn, fltcolumn)  values  (1095, 1095.1345);
insert into testdata(intcolumn, fltcolumn)  values  (5995, 5995.1345);
insert into testdata(intcolumn, fltcolumn)  values  (15, 15.1345);
insert into testdata(intcolumn, fltcolumn)  values  (25, 25.1345);
insert into testdata(intcolumn, fltcolumn)  values  (495, 495.1345);
insert into testdata(intcolumn, fltcolumn)  values  (35, 35.1345);
insert into testdata(intcolumn, fltcolumn)  values  (695, 695.1345);
insert into testdata(intcolumn, fltcolumn)  values  (595, 595.1345);
insert into testdata(intcolumn, fltcolumn)  values  (35, 35.1345);
insert into testdata(intcolumn, fltcolumn)  values  (30195, 30195.1345);
insert into testdata(intcolumn, fltcolumn)  values  (165, 165.1345);
insert into testdata(intcolumn, fltcolumn)  values  (65, 65.1345);
insert into testdata(intcolumn, fltcolumn)  values  (955, 955.1345);
insert into testdata(intcolumn, fltcolumn)  values  (135, 135.1345);
insert into testdata(intcolumn, fltcolumn)  values  (19195, 19195.1345);
insert into testdata(intcolumn, fltcolumn)  values  (145, 145.1345);
insert into testdata(intcolumn, fltcolumn)  values  (85, 85.1345);
insert into testdata(intcolumn, fltcolumn)  values  (455, 455.1345);

以下是比较结果:

ORACLE RESULTS
ORACLE RESULTS

select  percentile_cont(.25) within group (order by fltcolumn asc) myresult
from testdata;
select  percentile_cont(.75) within group (order by fltcolumn asc) myresult
from testdata;

myresult
- - - - - - - -
57.6345                

myresult
- - - - - - - -
760.1345               

POSTGRESQL RESULTS
POSTGRESQL RESULTS

select percentile_cont(array_agg(fltcolumn), 0.25) as myresult
from testdata;

select percentile_cont(array_agg(fltcolumn), 0.75) as myresult
from testdata;

myresult
real
57.6345

myresult
real
760.135

我希望通过不必重新发明轮子来帮助别人。

请享用!雷哈里斯


3
投票

使用PostgreSQL 9.4,现在支持百分位数,在有序集合聚合函数中实现:

percentile_cont(fraction) WITHIN GROUP (ORDER BY sort_expression) 

连续百分位数:返回与排序中指定分数对应的值,如果需要,在相邻输入项之间进行插值

percentile_cont(fractions) WITHIN GROUP (ORDER BY sort_expression)

multiple continuous percentile:返回与fractions参数的形状匹配的结果数组,每个非null元素由对应于该百分位数的值替换

有关更多详细信息,请参阅文档:http://www.postgresql.org/docs/current/static/functions-aggregate.html

© www.soinside.com 2019 - 2024. All rights reserved.