sql求所有分数不低于80分的学生+所有分数高于平均分的学生

假如成绩表t有学生student、科目project、成绩grade三个字段

1.求所有分数不低于80分的学生

方法1:

select student from t
where student not in (select student from t where score<80)
group by student

方法2:

select student,min(score) as min_score from t group by student having min_score>=80

 

2.求所有分数高于平均分的学生,这里平均分是所有科目所有成绩的总平均分

方法1:

select student from t
where student not in (select student from t where score<(select avg(score) from t))
group by student

方法2:可以用hive的窗口函数!想一下,所有分数高于平均分,就是学生最低的分数高于平均分

select student from
(
select student,min(score) over(partition by userid) as min_score,avg(score) over() as avg_score from t 
) x
where min_score>avg_score
group by student

 

3.假如是求所有科目分数都高于科目的平均分呢?

select student from t where student not in
(
	select student from
	(
		select student,avg(score) over(partition by project) as dangqian_project_avg_score,project,score from t 
	) x 
	where score<=dangqian_project_avg_score
) group by student

 

可以发现,窗口函数减少了not in,或者是join的次数。

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值