hive求成绩分析

26 篇文章 0 订阅 ¥29.90 ¥99.00

 

场景举例

北京市学生成绩分析

 

成绩的数据格式

exercise5_1.txt 文件中的每一行就是一个学生的成绩信息。字段之间的分隔符是","

时间,学校,年纪,姓名,科目,成绩


样例数据


 
 
  1. 2013,北大,1,黄渤,语文,97
  2. 2013,北大,1,徐峥,语文,52
  3. 2013,北大,1,刘德华,语文,85
  4. 2012,清华,0,马云,英语,61
  5. 2015,北理工,3,李彦宏,物理,81
  6. 2016,北科,4,马化腾,化学,92
  7. 2014,北航,2,刘强东,数学,70
  8. 2012,清华,0,刘诗诗,英语,59
  9. 2014,北航,2,刘亦菲,数学,49
  10. 2014,北航,2,刘嘉玲,数学,77

 

建表导数据


 
 
  1. create database if not exists exercise;
  2. use exercise;
  3. drop table if exists exercise5_1;
  4. create table exercise5_1( year int, school string, grade int, name string, course string, score int) row format delimited fields terminated by ',';
  5. load data local inpath "/home/hadoop/exercise5_1.txt" into table exercise5_1;
  6. select * from exercise5_1;
  7. desc exercise5_1;

 

 

需求

1、分组TopN,选出今年每个学校、每个年级、分数前三的科目


 
 
  1. select t.*
  2. from
  3. ( select school, grade, course, score,
  4. row_number() over ( partition by school, grade, course order by score desc) rank_code
  5. from exercise5_1
  6. where year = "2017"
  7. ) t
  8. where t.rank_code <= 3;

详解如下:
  row_number函数:row_number() 按指定的列进行分组生成行序列,从 1 开始,如果两行记录的分组列相同,则行序列 +1。
  over 函数:是一个窗口函数。
  over (order by score) 按照 score 排序进行累计,order by 是个默认的开窗函数。
  over (partition by grade) 按照班级分区。
  over (partition by grade order by score) 按照班级分区,并按着分数排序。
  over (order by score range between 2 preceding and 2 following) 窗口范围为当前行的数据幅度减2加2后的范围内的数据求和。

 

2、今年,北航,每个班级,每科的分数,及分数上下浮动2分的总和


 
 
  1. select school, grade, course, score,
  2. sum(score) over ( order by score range between 2 preceding and 2 following) sscore
  3. from exercise5_1
  4. where year = "2017" and school= "北航";

 

3、where与having:今年,清华1年级,总成绩大于200分的学生以及学生数


 
 
  1. select school, grade, name, sum(score) as total_score,
  2. count( 1) over ( partition by school, grade) nct
  3. from exercise5_1
  4. where year = "2017" and school= "清华" and grade = 1
  5. group by school, grade, name
  6. having total_score > 200;

having 是分组(group by)后的筛选条件,分组后的数据组内再筛选,也就是说 HAVING 子句可以让我们筛选成组后的各组数据。
where 则是在分组,聚合前先筛选记录。也就是说作用在 GROUP BY 子句和 HAVING 子句前。

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尬聊码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值