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

本文通过SQL查询演示了如何从数据表中找出所有学生的科目成绩均高于对应科目平均分的情况。首先创建成绩表,然后计算每个科目的平均分,接着设置标志位判断每个学生每科是否超过平均分,最后通过分组和求和验证学生所有科目是否全部高于平均分。查询结果显示,uid为1001的学生满足条件。
摘要由CSDN通过智能技术生成

表结构: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 score(
    uid string,
    subject_id string,
    score int)
row format delimited fields terminated by '\t'; 

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

select uid,
       subject_id,
       score,
       avg(score) over (distribute by subject_id) avg_score
from score
group by uid, subject_id, score;

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

with a as (
    select uid,
           subject_id,
           score,
           avg(score) over (distribute by subject_id) avg_score
    from score
    group by uid, subject_id, score
)
select uid,
       if(score > avg_score, 0, 1) flag
from a;

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

with a as (
    select uid,
           subject_id,
           score,
           avg(score) over (distribute by subject_id) avg_score
    from score
    group by uid, subject_id, score
),
     b as (select uid,
                  if(score > avg_score, 0, 1) flag
           from a)
select uid
from b
group by uid
having sum(b.flag) = 0;

换种写法 如下,结果是一样的

select uid
from (select uid,
             if(score > avg_score, 0, 1) flag
      from (select uid,
                   score,
                   avg(score) over (partition by subject_id) avg_score
            from score) t1) t2
group by uid
having sum(flag) = 0;

运行结果如下

+----+
|uid |
+----+
|1001|
+----+
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值