函数聚合函数聚合函数groupArraySamplegroupArraySample groupArraySample 自 v20.3 起引入 创建一个由参数值样本组成的数组。 结果数组的大小限制为 max_size 个元素。 参数值会被随机选择并添加到数组中。 语法 groupArraySample(max_size[, seed])(x) 参数 max_size — 结果数组的最大长度。UInt64 seed — 可选。随机数生成器的种子。默认值:123456。UInt64 x — 参数(列名或表达式)。Any 参数 array_column — 包含待聚合数组的列。Array 返回值 由随机选取的 x 参数组成的数组。Array(T) 示例 用法示例 CREATE TABLE default.colors ( id Int32, color String ) ENGINE = Memory; INSERT INTO default.colors VALUES (1, 'red'), (2, 'blue'), (3, 'green'), (4, 'white'), (5, 'orange'); SELECT groupArraySample(3)(color) as newcolors FROM default.colors; ┌─newcolors──────────────────┐ │ ['white','blue','green'] │ └────────────────────────────┘ 使用种子值的示例 -- Query with column name and different seed SELECT groupArraySample(3, 987654321)(color) as newcolors FROM default.colors; ┌─newcolors──────────────────┐ │ ['red','orange','green'] │ └────────────────────────────┘ 将表达式作为参数使用 -- Query with expression as argument SELECT groupArraySample(3)(concat('light-', color)) as newcolors FROM default.colors; ┌─newcolors───────────────────────────────────┐ │ ['light-blue','light-orange','light-green'] │ └─────────────────────────────────────────────┘