

# 从子查询创建数组
<a name="creating-arrays-from-subqueries"></a>

从一组行创建数组。

```
WITH
dataset AS (
  SELECT ARRAY[1,2,3,4,5] AS items
)
SELECT array_agg(i) AS array_items
FROM dataset
CROSS JOIN UNNEST(items) AS t(i)
```

此查询返回：

```
+-----------------+
| array_items     |
+-----------------+
| [1, 2, 3, 4, 5] |
+-----------------+
```

要从一组行创建唯一值的数组，请使用 `distinct` 关键字。

```
WITH
dataset AS (
  SELECT ARRAY [1,2,2,3,3,4,5] AS items
)
SELECT array_agg(distinct i) AS array_items
FROM dataset
CROSS JOIN UNNEST(items) AS t(i)
```

此查询返回以下结果。请注意，不保证排序。

```
+-----------------+
| array_items     |
+-----------------+
| [1, 2, 3, 4, 5] |
+-----------------+
```

有关使用 `array_agg` 函数的更多信息，请参阅 Trino 文档的[聚合函数](https://trino.io/docs/current/functions/aggregate.html)。