跳到主要内容
跳到主要内容

groupArraySample

创建一个样本参数值的数组。结果数组的大小限制为 max_size 元素。参数值是随机选择并添加到数组中的。

语法

groupArraySample(max_size[, seed])(x)

参数

  • max_size — 结果数组的最大大小。 UInt64
  • seed — 随机数生成器的种子。可选。 UInt64。 默认值: 123456
  • x — 参数(列名或表达式)。

返回值

  • 随机选择的 x 参数的数组。

类型: Array

示例

考虑表 colors

┌─id─┬─color──┐
│  1 │ red    │
│  2 │ blue   │
│  3 │ green  │
│  4 │ white  │
│  5 │ orange │
└────┴────────┘

以列名作为参数的查询:

SELECT groupArraySample(3)(color) as newcolors FROM colors;

结果:

┌─newcolors──────────────────┐
│ ['white','blue','green']   │
└────────────────────────────┘

以列名和不同种子进行的查询:

SELECT groupArraySample(3, 987654321)(color) as newcolors FROM colors;

结果:

┌─newcolors──────────────────┐
│ ['red','orange','green']   │
└────────────────────────────┘

以表达式作为参数的查询:

SELECT groupArraySample(3)(concat('light-', color)) as newcolors FROM colors;

结果:

┌─newcolors───────────────────────────────────┐
│ ['light-blue','light-orange','light-green'] │
└─────────────────────────────────────────────┘