查看所有系统函数
show functions;
函数分类
内置函数【系统函数】
数学函数:
floor、round、ceil、cos、log2等
字符串函数:
length、reverse、trim、lower、get_json_object、repeat等
收集函数:
size
转换函数:
cast
日期函数:
year、month、datediff、date、date_add等
条件函数:
coalesce、case…when、if等
聚合函数:
count、sum、avg、max、min等
表生成函数:
explode等
自定义函数
UDF
in:out = 1:1
UDAF
in:out = n:1
UDTF
in:out = 1:n
示例
使用 help 解决一个函数不知道怎么用的问题
desc function split;
count
统计记录行数
//最常见,但效率低
select count(*) from student;
//推荐使用,效率高
select count(1) from student;
if条件
if(test condition,true value,false value)
select if(12>20,'too little','too old');
coalesce
COALESCE( value1,value2,… )将参数列表中第 1 个不为 null 的值作为最后的值
select coalesce(null,null,2);
case…when
CASE [ expression ]
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
WHEN conditionn THEN resultn
ELSE result
END
select CASE 'apple'
WHEN 'apple' THEN 'this is apple'
WHEN 'pear' THEN 'this is not apple'
ELSE 'this is not fruit'
END
split
将字符串拆分成一个数组
select split("a,b,c",",");
explode
将一个集合元素,打散成一行一行的组成,即将一行改成多行,换句话说行转列
select explode(split("a,b,c",","));
lateral view
与explode联用,形成一张新表
创建用户得分表
create table user_score(
id int,
name string,
score_list string);
插入相应数据
insert into table user_score values(1,'one','90,80,70');
insert into table user_score values(2,'two','65,75,85');
insert into table user_score values(3,'three','55,40,85');
查看内部数据
select * from user_score;
通过 explode 将一行转换成多行,通过 lateral view 将多行转换成一个表
select id,name,score from user_score
lateral view explode(split(score_list,',')) score_table as score;
求分数大于80分的有多少人
select count(distinct id) from user_score
lateral view explode(split(score_list,',')) score_table as score
where score >=80;