hive归并查询——头歌

打印 上一主题 下一主题

主题 916|帖子 916|积分 2752

任务形貌

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

为了完本钱关任务,你须要掌握:1.hive多表查询,2.group by分组函数的使用。
多表查询
之前的单表查询只是对一张表进行查询,而多表查询须要将两张及两张以上的表进行关联查询。 在多表查询中,通常使用 表名.列名 来对各表中的列进行查询操作。 例如:一张info表,一张score表 info表
列名类型备注namestring姓名classstring班级 数据如下:
  1. [/code] [list=1]
  2. [*]zhangsan,c1
  3. [*]lisi,c2
  4. [*]wangwu,c3
  5. [*]zhaoliu,c2
  6. [*]donger,c1
  7. [*]xiaolin,c3
  8. [*]xiaoxuan,c2
  9. [*]zhouhao,c1
  10. [*]niuliu,c3
  11. [/list] score表
  12. [table][tr]列名类型备注[/tr][tr][td]name[/td][td]string[/td][td]姓名[/td][/tr][tr][td]score[/td][td]int[/td][td]分数[/td][/tr][/table] 数据如下:
  13. [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
代码如下
  1. create database if not exists info;
  2. use info;
  3. --创建stu_info表
  4. create table stu_info(
  5. class string,
  6. name string,
  7. sex string,
  8. profession string)
  9. row format delimited fields terminated by ","
  10. lines terminated by "\n" stored as textfile;
  11. --从本地导入数据到stu_info表中
  12. load data local inpath "/data/workspace/myshixun/studentinfo.txt" overwrite into table stu_info;
  13. --创建score表
  14. create table score(
  15. class string,
  16. name string,
  17. classid int,
  18. score int)
  19. row format delimited fields terminated by ","
  20. lines terminated by "\n" stored as textfile;
  21. --从本地导入数据到score表中
  22. load data local inpath "/data/workspace/myshixun/score.txt" overwrite into table score;
  23. --创建class表
  24. create table class (
  25. classid int,
  26. classname string)
  27. row format delimited fields terminated by ","
  28. lines terminated by "\n" stored as textfile;
  29. --从本地导入数据到class表中
  30. load data local inpath "/data/workspace/myshixun/class.txt" overwrite into table class;
  31. --查询各班学习Python的总人数
  32. 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企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王國慶

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表