目录
💟这里是CS大白话专场,让枯燥的学习变得有趣!
💟没有对象不要怕,我们new一个出来,每天对ta说不尽情话!
💟好记性不如烂键盘,自己总结不如收藏别人!
🧡 DML函数-拼接字符串
相关函数
🍠concat(str1, str2, …):返回输入字符串连接后的结果,支持任意个输入字符串。
🍠concat_ws(separator, str1, str2, ...):第一个参数separator为剩余参数间的分隔符。分隔符可以是与剩余参数一样的字符串,如果分隔符是 NULL,返回值也将为 NULL。这个函数会跳过分隔符参数后的 NULL 和空字符串。
注意: concat_ws must be "string or array<string>。
🍠collect_set(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生array类型字段。
案例
💌把星座和血型一样的人聚合到一起。
🍠vim person_info.txt 准备原始数据
🍠创建hive表并导入数据
create table person_info(
name string,
constellation string,
blood_type string)
row format delimited fields terminated by "\t";
load data local inpath "/home/jodie/datas/person_info.txt" into table person_info;
🍠按需查询
💌人名用数组保存
select
con_blood,
collect_set(name) name_arr
from
(select
concat(constellation,',',blood_type) con_blood,
name
from person_info)t1
group by con_blood;t2
💌人名用竖线隔开
select
con_blood,
concat_ws('|',name_arr)
from
(select
con_blood,
collect_set(name) name_arr
from
(select
concat(constellation,',',blood_type) con_blood,
name
from person_info)t1
group by con_blood)t2;
💌简写
select
con_blood,
concat_ws('|',collect_set(name))
from
(select
concat_ws(',',constellation,blood_type) con_blood,
name
from person_info)t1
group by con_blood;