当前位置:   article > 正文

4.HIVE函数_hive为空替换函数

hive为空替换函数

1.hive函数

1.1 空值替换

两个输入nvl(col,default_num) : 如果colum不为null,返回col.否则返回default_num

多个输入:coalesce(col1, col2, col3, ....) :从左到右找第一个不为null的值

例如:求所有员工的平均薪水

select avg(nvl(salary, 0 ))from emp;

 因为avg()会自动忽略null,这样可以保证null也参与了运算 

又例如:

select ename, job, sal, coalesce(job, sal, '啥也没有') from emp;

解释:如果有job, 就输出job;如果有sal,就输出sal,什么都没有就输出“啥都没有”

1.2 分支控制

数据准备:

  1. create table emp_sex(
  2. name string, --姓名
  3. dept_id string, --部门id
  4. sex string --性别
  5. )
  6. row format delimited fields terminated by "\t";
  1. load data local inpath '/opt/module/hive/datas/emp_sex.txt'
  2. into table emp_sex;

 1.2.1 if函数:

if (boolean, result1, result2) 如果boolean为真,返回result1,否则返回result2

例如:

 统计emp_sex表各部门男女的人数 

  1. select dpt_id
  2.       count(if(sex="男"),name,null)  male,
  3.       count(if(sex="女"),name,null)  female
  4. from emp_sex
  5. group by dpt_id;

1.2.2 case函数: 

  1. -- case col
  2. -- when value1 then result1
  3. -- when value2 then result2
  4. -- else result3
  5. -- end
  6. -- 如果col值为value1,返回result1;如果值为value2,返回result2;否则返回result3
  1. -- case when
  2. -- boolean1 then result1
  3. -- boolean2 then result2
  4. -- else result3
  5. -- end
  6. -- 如果boolean1为真,返回result1;如果boolean1为假,boolean2为真,返回result2
  7. 否则返回result3

 例如:

统计emp_sex表各部门男女的人数

  1. SELECT dept_id
  2. count(case sex when '男' then name else null end) male,
  3. count(case when sex='女' then name else null end) female
  4. from emp_sex
  5. group by dept_id;

1.3 行列转换

1.3.1 行转列

聚合函数colect_set(col)set会去重
聚合函数colect_list(col)list不会去重
字符串拼接concat(V1,V2,V3)字符串拼接

举例:

  1. -- 数据准备
  2. create table person_info(
  3. name string, -- 姓名
  4. constellation string, -- 星座
  5. blood_type string -- 血缘
  6. )
  7. row format delimited fields terminated by "\t";
  1. load data local inpath "/opt/module/hive/datas/constellation.txt"
  2. into table person_info;

 要求:把星座和血型一样的人归类到一起,结果如下:

ps.行转列:大海和凤姐以前是同一列的人,现在变成同一行了

初始思路:

  1. select constellation,
  2. blood_type,
  3. collect_list(name) names
  4. from person_info
  5. group by constellation, blood_type;

 进一步引申为:

  1. select concat(constellation, ',', blood_type) xzxx, //字符串拼接
  2. concat_ws('|', collect_list(name)) names //concat_WithSeperater
  3. from person_info
  4. group by constellation, blood_type;

1.3.2 列转行

数据准备:

 需求:将电影分类中的数组数据展开

                  

explode(array或map) :将一行输入变成多行多列,如果是array,就是一列,是map,就是多列

split(str, 分隔符) :将str按照指定分隔符分成字符串数组

使用格式:from 原表格 lateral view  UDTF函数 

  1. select m.movie,
  2. tbl.category_id
  3. from movie_info m
  4. lateral view explode(split(category, ',')) tbl as category_id;
  5. // explode后的表格命名为tbl,其中只有一列,命名为categroy_id

将上述表转化成category_id,movies:

ps.在原来的表上套一个子查询:

练习:根据下表,查一下结果name,child,age.

  1. select name ,child ,age
  2. from test lateral view  explode(children) tbl as child, age

2.练习题

准备数据集:

返回不同国家11月份的天气状况:

天气根据weather_state决定,avg(weather_state)<=15为寒冷,<25为温暖,>=25为炎热

通过子查询的方式来写:

先写子语句:

  1. select country_id,avg(weather_state) avg_w
  2. from weather
  3. where substring(day,1,7) ='2017-04'
  4. group by country_id;

然后写主语句:

  1. select  country_name
  2.             case  when avg_w<=15 then '寒冷'
  3.                   when  avg_w>15 and avg_w<=25 then '温暖'
  4.                   else '炎热‘ end
  5. from countries c
  6. join (
  7. select country_id,avg(weather_state) avg_w
  8. from weather  
  9. where substring(day,1,7) ='2019-11'
  10. group by country_id;
  11. ) t1
  12. on c.country_id = t1.country_id
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/973688
推荐阅读
相关标签
  

闽ICP备14008679号