collect_set(col)函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生array类型字段。例如,如下数据记录,要统计每种no下的score,这里就可以配合group by 产生奇效。
![eb812df8b916ef2e61d3b1e4333fd94f.png](https://img-blog.csdnimg.cn/img_convert/eb812df8b916ef2e61d3b1e4333fd94f.png)
select no,collect_set(score) from test_tmp_sy group by no;
![1b92464b03839602ab6d60e48b371412.png](https://img-blog.csdnimg.cn/img_convert/1b92464b03839602ab6d60e48b371412.png)
#返回一个列表[],‘,’隔开
collect_set,其与collect_list的区别就是会去重
select no,collect_list(score) from test_tmp_sy group by no;
![e4acfcd1411f9bb958ad7ade75bc230f.png](https://img-blog.csdnimg.cn/img_convert/e4acfcd1411f9bb958ad7ade75bc230f.png)
concat函数 连接函数
select concat(no,'-',score,'???') from test_tmp_sy;
![78c893efe1338795b1ea94cd7ef4834e.png](https://img-blog.csdnimg.cn/img_convert/78c893efe1338795b1ea94cd7ef4834e.png)
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。 第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
select concat_ws(',',no,score) from test_tmp_sy;
![b38c12adaad36bc54b80c161de7d7eb4.png](https://img-blog.csdnimg.cn/img_convert/b38c12adaad36bc54b80c161de7d7eb4.png)
str_to_map 用法 (把普通数组变为字典)
select str_to_map(concat_ws(',', collect_set(concat(no, '-',score))),',','-')
from test_tmp_sy
group by no
![e1e2715ad3ccd011c17b0d1a4ee607f1.png](https://img-blog.csdnimg.cn/img_convert/e1e2715ad3ccd011c17b0d1a4ee607f1.png)
![52703df728b2387444c1978cfe67fd22.png](https://img-blog.csdnimg.cn/img_convert/52703df728b2387444c1978cfe67fd22.png)