CREATE TABLE t1 (
c1 int,
c2 double,
c3 tinyint,
c4 int,
c5 bigint,
c6 largeint,
c7 string,
c8 double,
c9 date,
c10 datetime,
c11 array<int>,
c12 map<double, double>,
c13 struct<a bigint, b double>
)
DUPLICATE KEY(c1)
DISTRIBUTED BY HASH(c1)
BUCKETS 1
PROPERTIES ("replication_num" = "1");
insert into t1
select generate_series, generate_series, 11, 111, 1111, 11111, "111111", 1.1, "2024-09-01", "2024-09-01 18:00:00", [1, 2, 3], map(1, 5.5), row(100, 100)
from table(generate_series(1, 50000, 3));
-- 使用常量值作为权重
mysql> select percentile_approx_weighted(c1, 1, 0.9) from t1;
+----------------------------------------+
| percentile_approx_weighted(c1, 1, 0.9) |
+----------------------------------------+
| 45000.3984375 |
+----------------------------------------+
1 row in set (0.07 sec)
-- 使用数值列作为权重
mysql> select percentile_approx_weighted(c2, c1, 0.5) from t1;
+-----------------------------------------+
| percentile_approx_weighted(c2, c1, 0.5) |
+-----------------------------------------+
| 35355.97265625 |
+-----------------------------------------+
1 row in set (0.07 sec)
-- 使用权重和压缩比计算分位数
mysql> select percentile_approx_weighted(c2, c1, 0.5, 10000) from t1;
+------------------------------------------------+
| percentile_approx_weighted(c2, c1, 0.5, 10000) |
+------------------------------------------------+
| 35355.97265625 |
+------------------------------------------------+
1 row in set (0.09 sec)
mysql> select percentile_approx_weighted(c2, c1, [0.1, 0.5, 0.9], 10000) from t1;
+------------------------------------------------------------+
| percentile_approx_weighted(c2, c1, [0.1, 0.5, 0.9], 10000) |
+------------------------------------------------------------+
| [15811.6708984375,35355.97265625,47435.01171875] |
+------------------------------------------------------------+
1 row in set (0.03 sec)