2021-02-02 12:00:41 +00:00
|
|
|
---
|
2022-08-26 19:07:59 +00:00
|
|
|
slug: /zh/sql-reference/aggregate-functions/reference/grouparraysample
|
2022-04-10 23:08:18 +00:00
|
|
|
sidebar_position: 114
|
2021-02-02 12:00:41 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# groupArraySample {#grouparraysample}
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
构建一个参数值的采样数组。
|
|
|
|
结果数组的大小限制为 `max_size` 个元素。参数值被随机选择并添加到数组中。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
**语法**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
groupArraySample(max_size[, seed])(x)
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
**参数**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-05 15:50:14 +00:00
|
|
|
- `max_size` — 结果数组的最大长度。[UInt64](../../data-types/int-uint.md)。
|
|
|
|
- `seed` — 随机数发生器的种子。可选。[UInt64](../../data-types/int-uint.md)。默认值: `123456`。
|
|
|
|
- `x` — 参数 (列名 或者 表达式)。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
**返回值**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
- 随机选取参数 `x` (的值)组成的数组。
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-05 04:14:07 +00:00
|
|
|
类型: [Array](../../../sql-reference/data-types/array.md).
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
**示例**
|
2021-02-02 12:00:41 +00:00
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
样表 `colors`:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` text
|
|
|
|
┌─id─┬─color──┐
|
|
|
|
│ 1 │ red │
|
|
|
|
│ 2 │ blue │
|
|
|
|
│ 3 │ green │
|
|
|
|
│ 4 │ white │
|
|
|
|
│ 5 │ orange │
|
|
|
|
└────┴────────┘
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
使用列名做参数查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT groupArraySample(3)(color) as newcolors FROM colors;
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
```text
|
|
|
|
┌─newcolors──────────────────┐
|
|
|
|
│ ['white','blue','green'] │
|
|
|
|
└────────────────────────────┘
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
使用列名和不同的(随机数)种子查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT groupArraySample(3, 987654321)(color) as newcolors FROM colors;
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
```text
|
|
|
|
┌─newcolors──────────────────┐
|
|
|
|
│ ['red','orange','green'] │
|
|
|
|
└────────────────────────────┘
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
使用表达式做参数查询:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
``` sql
|
|
|
|
SELECT groupArraySample(3)(concat('light-', color)) as newcolors FROM colors;
|
|
|
|
```
|
|
|
|
|
2021-02-04 15:34:04 +00:00
|
|
|
结果:
|
2021-02-02 12:00:41 +00:00
|
|
|
|
|
|
|
```text
|
|
|
|
┌─newcolors───────────────────────────────────┐
|
|
|
|
│ ['light-blue','light-orange','light-green'] │
|
|
|
|
└─────────────────────────────────────────────┘
|
|
|
|
```
|