hive合并查询——头歌

任务描述

在之前的实训中,我们已经知道了Hive的单表查询,本关主要讲解如何进行多表查询。 本关任务:统计查询各班学习Python的人数。

相关知识

为了完成本关任务,你需要掌握:1.hive多表查询,2.group by分组函数的使用。

多表查询

之前的单表查询只是对一张表进行查询,而多表查询需要将两张及两张以上的表进行关联查询。 在多表查询中,通常使用 表名.列名 来对各表中的列进行查询操作。 例如:一张info表,一张scoreinfo

列名类型备注
namestring姓名
classstring班级

数据如下:

 
  1. zhangsan,c1
  2. lisi,c2
  3. wangwu,c3
  4. zhaoliu,c2
  5. donger,c1
  6. xiaolin,c3
  7. xiaoxuan,c2
  8. zhouhao,c1
  9. niuliu,c3

score

列名类型备注
namestring姓名
scoreint分数

数据如下:

 
  1. zhangsan,67
  2. lisi,83
  3. wangwu,57
  4. zhaoliu,86
  5. donger,63
  6. xiaolin,75
  7. xiaoxuan,92
  8. zhouhao,71
  9. niuliu,63

查询各班总成绩: select info.class,sum(score.score) from info,score where info.name=score.name group by info.class; 查询结果如下:

编程要求

根据提示,在右侧编辑器补充代码,统计查询各班学习Python的人数。 创建stu_info表:

列名类型备注
classstring班级
namestring姓名
sexstring性别
professionstring专业

score表:

列名类型备注
classstring班级
namestring姓名
classidint课程Id
scoreint分数

class表:

列名类型备注
classidint课程Id
classnamestring课程名

数据路径:

/data/workspace/myshixun/studentinfo.txt
/data/workspace/myshixun/class.txt /data/workspace/myshixun/score.txt 数据切分方式均为:英文逗号

测试说明

平台会对你编写的代码进行测试! 预期输出: c1 3 c3 2

代码如下

create database if not exists info;
use info;
--创建stu_info表
create table stu_info(
class string,
name string,
sex string,
profession string)
row format delimited fields terminated by ","
lines terminated by "\n" stored as textfile;
--从本地导入数据到stu_info表中
load data local inpath "/data/workspace/myshixun/studentinfo.txt" overwrite into table stu_info;
--创建score表
create table score(
class string,
name string,
classid int,
score int)
row format delimited fields terminated by ","
lines terminated by "\n" stored as textfile;
--从本地导入数据到score表中
load data local inpath "/data/workspace/myshixun/score.txt" overwrite into table score;
--创建class表
create table class (
classid int,
classname string)
row format delimited fields terminated by ","
lines terminated by "\n" stored as textfile;
--从本地导入数据到class表中
load data local inpath "/data/workspace/myshixun/class.txt" overwrite into table class;
--查询各班学习Python的总人数
select score.class,count(score.classid) from score,class where class.classname=="Python" and class.classid=score.classid group by score.class;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值