percentile_disc
返回基于输入列 expr
的离散分布的百分位数值。 如果找不到精确的百分位数值,此函数将返回两个最接近值之间的较大值。
此函数从 v2.5 版本开始支持。
语法
PERCENTILE_DISC (expr, percentile)
参数
expr
:要计算百分位数值的列。 该列可以是任何可排序的数据类型。percentile
:要查找的值的百分位数。 它必须是介于 0 和 1 之间的常量浮点数。 例如,如果要查找中值,请将此参数设置为0.5
。 如果要查找第 70 个百分位的值,请指定 0.7。
返回值
返回值的数据类型与 expr
相同。
使用说明
NULL 值在计算中将被忽略。
示例
创建表 exam
并将数据插入此表。
CREATE TABLE exam (
subject STRING,
score INT
)
DISTRIBUTED BY HASH(`subject`);
INSERT INTO exam VALUES
('chemistry',80),
('chemistry',100),
('chemistry',null),
('math',60),
('math',70),
('math',85),
('physics',75),
('physics',80),
('physics',85),
('physics',99);
select * from exam order by subject;
+-----------+-------+
| subject | score |
+-----------+-------+
| chemistry | 80 |
| chemistry | 100 |
| chemistry | NULL |
| math | 60 |
| math | 70 |
| math | 85 |
| physics | 75 |
| physics | 80 |
| physics | 85 |
| physics | 99 |
+-----------+-------+
计算每个科目的中位数。
select subject, percentile_disc(score, 0.5)
from exam group by subject;
输出
+-----------+-----------------------------+
| subject | percentile_disc(score, 0.5) |
+-----------+-----------------------------+
| chemistry | 100 |
| math | 70 |
| physics | 85 |
+-----------+-----------------------------+