any_value
功能
在包含 GROUP BY
的聚合查询中,该函数用于从每个聚合分组中随机选择一行返回。
语法
ANY_VALUE(expr)
参数说明
expr
: 选取的表达式。从 3.2 版本起,expr
支持 ARRAY/MAP/STRUCT 类型。
返回值说明
在每个聚合后的分组中随机选择某行的结果返回,结果是不确定的。
示例
假设有如下表。
CREATE TABLE t0(
a INT,
b BIGINT,
c SMALLINT,
d ARRAY<INT>,
e JSON,
f MAP<INT, INT>,
g STRUCT<a STRING, b INT>
)
DUPLICATE KEY(a)
DISTRIBUTED BY HASH(a);
INSERT INTO t0 VALUES
(1, 1, 1, [2,3,4],parse_json('{"a":1, "b":true}'), map{1:1,3:4}, row(1, 2)),
(1, 2, 1, [2,3,5],parse_json('{"a":2, "b":true}'), map{1:2,3:3},row(2, 2)),
(2, 1, 1, [2,3,6],parse_json('{"a":3, "b":true}'), map{2:1,3:2},row(3, 2)),
(2, 2, 2, [2,4,5],parse_json('{"a":4, "b":false}'),map{1:3,3:1},row(4, 2)),
(3, 1, 1, [3,3,5],parse_json('{"a":5, "b":false}'),map{2:1,3:3},row(1, 2));
mysql> select * from t0 order by a;
+------+------+------+---------+----------------------+-----------+-----------------+
| a | b | c | d | e | f | g |
+------+------+------+---------+----------------------+-----------+-----------------+
| 1 | 1 | 1 | [2,3,4] | {"a": 1, "b": true} | {1:1,3:4} | {"a":"1","b":2} |
| 1 | 2 | 1 | [2,3,5] | {"a": 2, "b": true} | {1:2,3:3} | {"a":"2","b":2} |
| 2 | 1 | 1 | [2,3,6] | {"a": 3, "b": true} | {2:1,3:2} | {"a":"3","b":2} |
| 2 | 2 | 2 | [2,4,5] | {"a": 4, "b": false} | {1:3,3:1} | {"a":"4","b":2} |
| 3 | 1 | 1 | [3,3,5] | {"a": 5, "b": false} | {2:1,3:3} | {"a":"1","b":2} |
+------+------+------+---------+----------------------+-----------+-----------------+
5 rows in set (0.01 sec)
使用 ANY_VALUE 后的结果。可以看到对于 a=1
和 a=2
,随机返回了一条 b
记录。
mysql> select a,any_value(b),sum(c) from t0 group by a;
+------+----------------+----------+
| a | any_value(`b`) | sum(`c`) |
+------+----------------+----------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 1 |
+------+----------------+----------+
3 rows in set (0.01 sec)
mysql> select a,any_value(d),sum(b) from t0 group by a;
+------+--------------+--------+
| a | any_value(d) | sum(b) |
+------+--------------+--------+
| 3 | [3,3,5] | 1 |
| 1 | [2,3,4] | 3 |
| 2 | [2,3,6] | 3 |
+------+--------------+--------+
2 rows in set (0.01 sec)