QUANTILE_PERCENT
更新时间:2025-10-16
Description
QUANTILE_PERCENT 函数用于计算给定百分比的分位数值。它接受两个参数:一个 quantile_state 列和一个表示百分比的常量浮点数。该函数返回一个浮点数,表示给定百分比位置的分位数值。
Syntax
                SQL
                
            
            1QUANTILE_PERCENT(<quantile_state>, <percent>)Parameters
| 参数 | 描述 | 
|---|---|
| <quantile_state> | 目标列。 | 
| <percent> | 目标百分比。 | 
Return value
返回一个 Double 类型的分位数值。
Example
                SQL
                
            
            1CREATE TABLE IF NOT EXISTS quantile_state_agg_test (
2         `dt` int(11) NULL COMMENT "",
3         `id` int(11) NULL COMMENT "",
4         `price` quantile_state QUANTILE_UNION NOT NULL COMMENT ""
5        ) ENGINE=OLAP
6        AGGREGATE KEY(`dt`, `id`)
7        COMMENT "OLAP"
8        DISTRIBUTED BY HASH(`dt`) BUCKETS 1
9        PROPERTIES ("replication_num" = "1");
10
11INSERT INTO quantile_state_agg_test VALUES(20220201,0, to_quantile_state(1, 2048));
12
13INSERT INTO quantile_state_agg_test VALUES(20220201,1, to_quantile_state(-1, 2048)),
14            (20220201,1, to_quantile_state(0, 2048)),(20220201,1, to_quantile_state(1, 2048)),
15            (20220201,1, to_quantile_state(2, 2048)),(20220201,1, to_quantile_state(3, 2048));
16
17SELECT dt, id, quantile_percent(quantile_union(price), 0) FROM quantile_state_agg_test GROUP BY dt, id ORDER BY dt, id结果为
                Text
                
            
            1+----------+------+--------------------------------------------+
2| dt       | id   | quantile_percent(quantile_union(price), 0) |
3+----------+------+--------------------------------------------+
4| 20220201 |    0 |                                          1 |
5| 20220201 |    1 |                                         -1 |
6+----------+------+--------------------------------------------+