当前位置:   article > 正文

Hive QL解题流程详解一_(1)在hive中创建student数据库,并且在该库中创建score,course和teacher

(1)在hive中创建student数据库,并且在该库中创建score,course和teacher表,导入数

第1题

表结构:uid,subject_id,score

求:找出所有科目成绩都大于某一学科平均成绩的学生

分析:
    要求每个学生的平均成绩
    每个学生的每科成绩和平均成绩进行比较

数据集如下

1001   01 90
1001   02 90
1001   03 90
1002   01 85
1002   02 85
1002   03 70
1003   01 70
1003   02 70
1003   03 85

1) 建表语句

create table if not exists score(
uid string,
subject_id string,
`score` int)
row format delimited fields terminated by '\t';

2) 求出每个学科的平均成绩

select
`uid`,
`score`,
avg(score) over(partition by subject_id) avg_score
from
score;t1

3) 根据是否大于平均成绩记录flag,大于则记为0否则记为1

select
`uid`
if(score>avg_score,0,1) flag
from
t1;t2

4) 根据学生id进行分组统计flag的和,和为0就是所有学科都大于平均成绩

select
uid
fro
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/696443
推荐阅读
相关标签
  

闽ICP备14008679号