0.函数查询
1.显示所有系统函数
show functions;
2.查询有关日期的函数
show functions like "*date*"
3.查看函数说明
desc function 'current_date';
一.空字段赋值
给值为NULL的数据赋值,它的格式是NVL( value,default_value)。它的功能是如果value为NULL,则NVL函数返回default_value的值,否则返回value的值,如果两个参数都为NULL ,则返回NULL。
将comm列为null的值赋值为-1
select *,nvl(comm, -1) from emp;
二.CASE WHEN:类switch case
创建表,上传数据
create table emp_sex(
name string,
dept_id string,
sex string)
row format delimited fields terminated by "\t";
load data local inpath '/opt/module/datas/emp_sex.txt' into table emp_sex;
1.先统计各部门有多人
select dept_id,
count(*) total,
from emp_sex
group by dept_id;
2.求出不同部门男女各多少人
select dept_id,
count(*) total,
sum(case sex when '男' then 1 else 0 end) male,
sum(case sex when '女' then 1 else 0 end) famale
from emp_sex
group by dept_id;
三.行转列
使用函数:
CONCAT(string A/col, string B/col…):返回输入字符串连接后的结果,支持任意个输入字符串;
CONCAT_WS(separator, str1, str2,...):它是一个特殊形式的 CONCAT()。第一个参数剩余参数间的分隔符。分隔符可以是与剩余参数一样的字符串。如果分隔符是 NULL,返回值也将为 NULL。这个函数会跳过分隔符参数后的任何 NULL 和空字符串。分隔符将被加到被连接的字符串之间;
COLLECT_SET(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生array类型字段。
COLLECT_LIST:不去重
需求:把星座和血型一样的人归类到一起
射手座,A 大海|凤姐
白羊座,A 孙悟空|猪八戒
白羊座,B 宋宋|苍老师
创建表上传数据
create table person_info(
name string,
constellation string,
blood_type string)
row format delimited fields terminated by "\t";
load data local inpath "/opt/module/datas/constellation.txt" into table person_info;
1.先统计相同星座,及血型各有多少人使用函数“count()”
select constellation,blood_type,count(*)
from person_info
group by constellation,blood_type;