一,前言

二,连接查询

  2.1 内连接(join | inner join)

  2.2 左外连接(left join | left outer join)

  2.3 右外连接(right join | right outer join)

  2.4 全外连接(full join | full outer join)

  2.5 左半连接(left semi join)

三,分组聚合

  3.1 分组聚合须知

  3.2 分组聚合练习1

  3.3 分组聚合练习2

四,条件判断

  4.1 case...when...

  4.2 if判断

 

 

 

 

 

 

正文

  表创建和数据插入:

create table t_access(ip string,url string,access_time string)
partitioned by (dt string)
row format delimited fields terminated by ',';


-- 导入数据
load data local inpath '/root/hivetest/access.log.0804' into table t_access partition(dt='2017-08-04');
load data local inpath '/root/hivetest/access.log.0805' into table t_access partition(dt='2017-08-05');
load data local inpath '/root/hivetest/access.log.0806' into table t_access partition(dt='2017-08-06');

-- 查看表的分区 show partitions t_access;

  点击下载

4.1 case...when...

  语法结构:

case  // 开始提示符
    when 条件1 then 符合条件1的结果
    when 条件2 then 符合条件2的结果
    else 上述都不符合的结果
end   //结束提示符

  表创建和数据导入:

create table t_user(id int,name string,info struct<age:int,sex:string,addr:string>)
row format delimited fields terminated by ','
collection items terminated by ':';

-- 导入数据
load data local inpath '/root/hivetest/user.dat' into table t_user;

  数据查询:

select * from t_user;
+------------+--------------+----------------------------------------+--+
| t_user.id  | t_user.name  |              t_user.info               |
+------------+--------------+----------------------------------------+--+
| 1          | zhangsan     | {"age":18,"sex":"male","addr":"深圳"}    |
| 2          | lisi         | {"age":28,"sex":"female","addr":"北京"}  |
| 3          | wangwu       | {"age":38,"sex":"male","addr":"广州"}    |
| 4          | 赵六           | {"age":26,"sex":"female","addr":"上海"}  |
| 5          | 钱琪           | {"age":35,"sex":"male","addr":"杭州"}    |
| 6          | 王八           | {"age":48,"sex":"female","addr":"南京"}  |
+------------+--------------+----------------------------------------+--+

  需求:查询出用户的id、name、年龄(如果年龄在30岁以下,显示年轻人,30-40之间,显示中年人,40以上老年人)

  需求实现:

select id,name,
case
  when info.age<30 then '青年'
  when info.age>=30 and info.age<40 then '中年'
  else '老年'
end
from t_user;

4.2 if判断

  语法结构:

if(条件,条件成立结果,条件不成立结果)

  表创建和数据导入:

-- 建表映射:
create table t_movie(movie_name string,actors array<string>,first_show date)
row format delimited fields terminated by ','
collection items terminated by ':';

-- 导入数据
load data local inpath '/root/hivetest/actor.dat' into table t_movie;

  数据查询:

select * from t_movie;

+---------------------+------------------------+---------------------+--+
| t_movie.movie_name  |     t_movie.actors     | t_movie.first_show  |
+---------------------+------------------------+---------------------+--+
| 战狼2                 | ["吴京","吴刚","龙母"]       | 2017-08-16          |
| 三生三世十里桃花            | ["刘亦菲","痒痒"]           | 2017-08-20          |
| 普罗米修斯               | ["苍老师","小泽老师","波多老师"]  | 2017-09-17          |
| 美女与野兽               | ["吴刚","加藤鹰"]           | 2017-09-17          |
+---------------------+------------------------+---------------------+--+

  需求:查询电影信息,并且如果主演中有吴刚的,显示好电影,否则烂片

  需求实现:

select movie_name,actors,first_show,
if(array_contains(actors,'吴刚'),'好片儿','烂片儿')
from t_movie;