如何使用Presto在字符串列中获取第一个元组

发布于 2025-01-30 16:48:42 字数 588 浏览 4 评论 0原文

因此,我在表中有一个列,该列的数据类型是VARCHAR,但其中包含一组元组,因此我需要提取表中的阵列的第一个元组,

这是原始表

用户ID注释。
1[[“ Hello World”,1],[“您如何”,1],[这是一个“一个”,1]]
2[[“ Hello”,1],[How''how'',1],, [“ this”,1]]

这就是我正在寻找的东西,请注意,'评论'列的数据类型是 varchar

用户ID评论
1Hello World
2Hello

so i am having a column in the table, the data type of the column is varchar, but it contains an array of tuples, so what I need is to extract the first tuple of the array in the table

this is the original table

useridcomments
1[["hello world",1],["How did you",1],[" this is the one",1]]
2[["hello ",1],["How ",1],[" this",1]]

and this is what i am looking for , please notice that the datatype of 'comments' column is varchar.

useridcomments
1hello world
2hello

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

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

发布评论

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

评论(1

凝望流年 2025-02-06 16:48:42

WITH dataset (userid, comments) AS (
    VALUES  (1, json '[["hello world",1],["How did you",1],[" this is the one",1]]'),
            (2, json '[["hello ",1],["How ",1],[" this",1]]')
)

--query
select userid,
    json_extract_scalar(comments, '$[0][0]')
from dataset

userId注释
1Hello World
2Hello 2

请注意,如果您需要多个值,则只需提取单个值(类似于完成的一个在这里但是使用数组,例如array(json))。

json_extract_scalar should do the trick:

WITH dataset (userid, comments) AS (
    VALUES  (1, json '[["hello world",1],["How did you",1],[" this is the one",1]]'),
            (2, json '[["hello ",1],["How ",1],[" this",1]]')
)

--query
select userid,
    json_extract_scalar(comments, '$[0][0]')
from dataset

Output:

useridcomments
1hello world
2hello

Note that it will allow to extract only single value, if you want multiple values you will need to do some casting (similar to one done here but using arrays, for example array(json)).

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