任务形貌
在之前的实训中,我们已经知道了Hive的单表查询,本关主要讲授怎样进行多表查询。 本关任务:统计查询各班学习Python的人数。
相关知识
为了完本钱关任务,你须要掌握:1.hive多表查询,2.group by分组函数的使用。
多表查询
之前的单表查询只是对一张表进行查询,而多表查询须要将两张及两张以上的表进行关联查询。 在多表查询中,通常使用 表名.列名 来对各表中的列进行查询操作。 例如:一张info表,一张score表 info表
列名类型备注namestring姓名classstring班级 数据如下:
- [/code] [list=1]
- [*]zhangsan,c1
- [*]lisi,c2
- [*]wangwu,c3
- [*]zhaoliu,c2
- [*]donger,c1
- [*]xiaolin,c3
- [*]xiaoxuan,c2
- [*]zhouhao,c1
- [*]niuliu,c3
- [/list] score表
- [table][tr]列名类型备注[/tr][tr][td]name[/td][td]string[/td][td]姓名[/td][/tr][tr][td]score[/td][td]int[/td][td]分数[/td][/tr][/table] 数据如下:
- [code]
复制代码
- zhangsan,67
- lisi,83
- wangwu,57
- zhaoliu,86
- donger,63
- xiaolin,75
- xiaoxuan,92
- zhouhao,71
- 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课程Idscoreint分数 class表:
列名类型备注classidint课程Idclassnamestring课程名 数据路径:
/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;
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |