gpt4 book ai didi

sql - 在 Hive 中使用定界符连接多行

转载 作者:可可西里 更新时间:2023-11-01 14:11:01 25 4
gpt4 key购买 nike

我需要使用“~”作为分隔符逐行连接字符串值。我有以下数据:

enter image description here

我需要按照 'row_id' 的升序为每个 'id' 连接 'Comment' 列,并以 '~' 作为分隔符。

预期输出如下:

enter image description here

GROUP_CONCAT 不是一个选项,因为它在我的 Hive 版本中无法识别。我可以使用 collect_setcollect_list,但我无法在两者之间插入定界符。

有什么解决方法吗?

最佳答案

collect_list 返回数组,而不是字符串。
可以使用 concat_ws 将数组转换为定界字符串。


这会起作用,没有特定的注释顺序。

select      id
,concat_ws('~',collect_list(comment)) as comments

from mytable

group by id
;

+----+-------------+
| id | comments |
+----+-------------+
| 1 | ABC~PRQ~XYZ |
| 2 | LMN~OPQ |
+----+-------------+

关于sql - 在 Hive 中使用定界符连接多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42949153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com