1、concat()
1.1、用法:concat(str1,str2,str3,…)
連接參數(shù)的函數(shù),返回結(jié)果為連接參數(shù)的字符串。如果有一個(gè)參數(shù)為NULL
,則返回的結(jié)果為NULL
。
1.2、示例
concat('a', 'b', 'c') ---- 'abc'
concat('a', null, 'c')----null
2、concat_ws()
2.1、用法:concat_ws('分隔符', str1, str2, …)
concat()
的一個(gè)特殊形式,表示concat with separator
,兩個(gè)參數(shù)之間加上特定的分隔符。返回的是用指定分隔符連接參數(shù)的字符串。如果分割符為null
,則返回null
,參數(shù)為null
,則忽略該參數(shù)。文章來源:http://www.zghlxwxcb.cn/news/detail-782601.html
2.2、示例
concat_ws("/", "2018", "12", "19")----2018/12/19
concat_ws(":", "22", "47", null)----22:47
concat_ws(null, "22", "47")----null
3、group_concat()
3.1、用法:group_concat(str1, [order by str3], [separator '分隔符'])
把相同組的字符串以特定分隔符連接為一行。文章來源地址http://www.zghlxwxcb.cn/news/detail-782601.html
3.2、示例
3.2.1、數(shù)據(jù)
id |
name |
1 | bob |
1 | anna |
1 | helen |
2 | tom |
2 | baby |
2 | tom |
3.2.2、按id
分組,把name
連接為1行
select id, group_concat(name)
1 | bobannahelen |
2 | tombabytom |
3.2.3、按id
分組,把name
連接為一行,并按name
升序
select id,group_concat(name order by name asc)
1 | annabobhelen |
2 | babytomtom |
3.2.4、按id
分組,name
去重并連接為一行,按name
升序,用逗號(hào)分隔
select id,group_concat(name order by name asc)
1 | anna,bob,helen |
2 | baby,tom |
到了這里,關(guān)于Hive中的常用concat函數(shù)——concat函數(shù)、concat_ws函數(shù)和group_concat函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!