在Mysql中可以通过group_concat()
函数实现分组字符串拼接,在HIVE SQL中可以使用concat_ws()+collect_set()/collect_list()
函数实现相同的效果。
实例:
a | b | c |
---|---|---|
2014 | B | 9 |
2015 | A | 8 |
2014 | A | 10 |
2015 | B | 7 |
2014 | B | 6 |
1.concat_ws+collect_list 非去重拼接
select a
,concat_ws('-',collect_list(b)) as col_b
,concat_ws('-',collect_list(cast(c as string))) as col_c
from tb_name
group by a
;
查询结果:
a | col_b | col_c |
---|---|---|
2014 | B-A-B | 9-10-6 |
2015 | A-B | 8-7 |
2.concat_ws+collect_set 去重拼接
select a
,concat_ws('-',collect_set(b)) as col_b
,concat_ws('-',collect_set(cast(c as string))) as col_c
from tb_name
group by a
;
查询结果:
a | col_b | col_c |
---|---|---|
2014 | B-A | 9-10-6 |
2015 | A-B | 8-7 |
3.如果在分组拼接的时候需要保持拼接结果为有序排序,可以通过以下两种方式
1)先排序再拼接
select a
,concat_ws('-',collect_set(b)) as col_b
,concat_ws('-',collect_set(cast(c as string))) as col_c
from
(
select a
,b
,c
,row_number() over (partition by a order by b asc) as rn
from tb_name
) t
group by a
;
查询结果:
a | col_b | col_c |
---|---|---|
2014 | A-B | 10-6-9 |
2015 | A-B | 8-7 |
select a
,concat_ws('-',collect_list(b)) as col_b
,concat_ws('-',collect_list(cast(c as string))) as col_c
from
(
select a
,b
,c
,row_number() over (partition by a order by b asc) as rn
from tb_name
) t
group by a
;
查询结果:(col_b与col_c 的位置对应且col_b中的字符有序)文章来源:https://www.toymoban.com/news/detail-657476.html
a | col_b | col_c |
---|---|---|
2014 | A-B-B | 10-6-9 |
2015 | A-B | 8-7 |
2)sort_array()函数升序排列文章来源地址https://www.toymoban.com/news/detail-657476.html
select a
,concat_ws('-',sort_array(collect_list(b))) as col_b
from tb_name
group by a
;
a | col_b |
---|---|
2014 | A-B-B |
2015 | A-B |
到了这里,关于HIVE SQL实现分组字符串拼接concat的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!