如何在Presto/Hive中使用SQL查询创建数组?

发布于 2025-02-12 04:00:16 字数 669 浏览 1 评论 0原文

有两个记录:

类型名称
果实苹果
果橙橙色

我需要在presto中使用SQL查询:

键入名称
果实[“苹果”,“橙色”]

我该如何编写SQL查询以获取桌子上方?

There are two records:

Typename
FruitApple
FruitOrange

I need below output using sql query in presto:

Typename
Fruit["Apple","Orange"]

How can I write the sql query to get above table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2025-02-19 04:00:16

聚合函数 array_agg特别是设计了为此目的:

--sample data
WITH dataset(type, name) AS (
    VALUES ('Fruit', 'Apple'),
           ('Fruit', 'Orange')
)

-- sample query
SELECT type, array_agg(name) name
FROM dataset
group by type

输出:

键入名称
水果[苹果,橙色]

Aggregate function array_agg is designed especially for this purpose:

--sample data
WITH dataset(type, name) AS (
    VALUES ('Fruit', 'Apple'),
           ('Fruit', 'Orange')
)

-- sample query
SELECT type, array_agg(name) name
FROM dataset
group by type

Output:

typename
Fruit[Apple, Orange]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文