hivesql 如何實現分組后拼接同一列的字符串呢?
分組之后,在組內對某一列做字符串拼接,效果如下:
要知道,拼接幾列字符串不難,concat 就可以實現,concat(col1,col2,col3) 就會將第一列,第二列,第三列字符拼接起來,此處就不做示范了。
但如果要拼接一列中的字符呢?小編是沒有遇見過啦,所以懵的一逼。搜索了下,發現了一個很好用的函數
concat_ws
select id, concat_ws('_',collect_set(col1)) as concatcol1 from table group by id
小編試了之后不成功,於是換了第二種函數 groupconcat
select id,group_concat(distinct(col1),'_') as concatcol1 from table group by id
實現了在組內,拼接同一列字符串。