PERCENTILE_CONT 函数 - Amazon Redshift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异，请参阅中国的 Amazon Web Services 服务入门

# PERCENTILE_CONT 函数

PERCENTILE_CONT 是一种假定连续分布模型的逆分布函数。该函数具有一个百分比值和一个排序规范，并返回一个在有关排序规范的给定百分比值范围内的内插值。

PERCENTILE_CONT 在对值进行排序后计算值之间的线性内插。通过在聚合组中使用百分比值 (P) 和非 null 行数 (N)，该函数会在根据排序规范对行进行排序后计算行号。根据公式 (RN) 计算此行号 RN = (1+ (P*(N-1))。聚合函数的最终结果通过行号 CRN = CEILING(RN)FRN = FLOOR(RN) 的行中的值之间的线性内插计算。

(CRN - RN) * (value of expression for row at FRN) + (RN - FRN) * (value of expression for row at CRN).

PERCENTILE_CONT 是仅计算节点函数。如果查询不引用用户定义的表或 Amazon Redshift 系统表，该函数将返回错误。

## Syntax

PERCENTILE_CONT ( percentile ) WITHIN GROUP (ORDER BY expr)

## Arguments

percentile

WITHIN GROUP ( ORDER BY expr)

## Returns

INT2、INT4、INT8、NUMERIC、DECIMAL DECIMAL
FLOAT、DOUBLE DOUBLE
DATE DATE
TIMESTAMP TIMESTAMP
TIMESTAMPTZ TIMESTAMPTZ

## 使用说明

select top 10 salesid, sum(pricepaid), percentile_cont(0.6) within group (order by salesid), median (pricepaid) from sales group by salesid, pricepaid; An error occurred when executing the SQL command: select top 10 salesid, sum(pricepaid), percentile_cont(0.6) within group (order by salesid), median (pricepaid) from sales group by salesid, pricepai... ERROR: within group ORDER BY clauses for aggregate functions must be the same

select top 10 salesid, sum(pricepaid), percentile_cont(0.6) within group (order by salesid), median (salesid) from sales group by salesid, pricepaid;

## Examples

select top 10 distinct sellerid, qtysold, percentile_cont(0.5) within group (order by qtysold), median (qtysold) from sales group by sellerid, qtysold; sellerid | qtysold | percentile_cont | median ---------+---------+-----------------+------- 1 | 1 | 1.0 | 1.0 2 | 3 | 3.0 | 3.0 5 | 2 | 2.0 | 2.0 9 | 4 | 4.0 | 4.0 12 | 1 | 1.0 | 1.0 16 | 1 | 1.0 | 1.0 19 | 2 | 2.0 | 2.0 19 | 3 | 3.0 | 3.0 22 | 2 | 2.0 | 2.0 25 | 2 | 2.0 | 2.0