原始数据:
id score
aaa 1
aaa 2
aaa 3
预期结果:
id score
aaa 1,2,3
可使用
select id,concat_ws(',',collect_set(cast(colname as string)))
from table group by id;
使用concat_ws函数,需将字段转成string格式,collect_set会对该列进行去重,如果不需要去重,可使用collect_list参数代替。
本文介绍了一种使用SQL的concat_ws函数结合collect_set或collect_list参数,对数据进行聚合的方法,特别适用于需要将同一ID下的多个值合并为一个字符串场景。通过具体示例,展示了如何将多个分数值整合到单一行中。
原始数据:
id score
aaa 1
aaa 2
aaa 3
预期结果:
id score
aaa 1,2,3
可使用
select id,concat_ws(',',collect_set(cast(colname as string)))
from table group by id;
使用concat_ws函数,需将字段转成string格式,collect_set会对该列进行去重,如果不需要去重,可使用collect_list参数代替。
3996
607
2235

被折叠的 条评论
为什么被折叠?