赞
踩
1. concat() 实现把若干个字段(字段类型可不相同)数据拼接起来
- 用法: concat(string a1, int a2, float a3)
- select concat("aa", 11, 2.2);
- aa112.2
-
- 不同字段之间用分隔符连接("_")
- select concat("aa","_",11,"_",2.2);
- aa_11_2.2
举例:计算各个类别占比
- select sourcetype
- ,concat(round(count(1)*100.0/total,2),"%") as rate
- from table
- group by sourcetype;
2. concat_ws() 使用分隔符将若干个字符串拼接起来,实现“列转行”
- 用法: 其第一个参数是分隔符,第二个及之后的参数是待拼接数据,
- 类型 must be "string or array<string>"。
- 1. concat_ws(seperator, string s1, string s2,...)
-
- select concat_ws("_","aa",string(11),string(2.2));
- aa_11_2.2
-
- 2.常常结合group by与collect_set,collect_list使用
-
- 表数据为
- id score
- 11 100
- 11 80
- 22 90
- 22 80
- 22 70
- select id
- ,concat_ws("_", collect_list(cast(score as string)))
- from table
- group by id;
-
- 11 100_80
- 22 90_80_70

cast(value as type) 将某个列的值显示的转化为某个类型
例子:cast(score as string ) 将int类型的数据转化为了String类型
collect_set() 与group by结合,将一个字段元素形成一个集合(元素自动去重),
与contact_ws结合使用就是将这些元素以指定分隔符拼接成成字符串。
collect_list() 与collect_set() 功能类似,但不会对元素去重。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。