1、concat()
1.1、用法:concat(str1,str2,str3,…)
连接参数的函数,返回结果为连接参数的字符串。如果有一个参数为NULL
,则返回的结果为NULL
。
1.2、示例
concat('a', 'b', 'c') ---- 'abc'
concat('a', null, 'c')----null
2、concat_ws()
2.1、用法:concat_ws('分隔符', str1, str2, …)
concat()
的一个特殊形式,表示concat with separator
,两个参数之间加上特定的分隔符。返回的是用指定分隔符连接参数的字符串。如果分割符为null
,则返回null
,参数为null
,则忽略该参数。文章来源:https://www.toymoban.com/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 '分隔符'])
把相同组的字符串以特定分隔符连接为一行。文章来源地址https://www.toymoban.com/news/detail-782601.html
3.2、示例
3.2.1、数据
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
升序,用逗号分隔
select id,group_concat(name order by name asc)
1 | anna,bob,helen |
2 | baby,tom |
到了这里,关于Hive中的常用concat函数——concat函数、concat_ws函数和group_concat函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!