HQL练习1

表结构: uid,subject_id,score
求: 找出所有科目成绩都大于某一学科平均成绩的用户

建表语句

1create table if not exists score(uid string,subject string,score int) 2row format delimited 3fields terminated by '/t';

t表+------+--+
| uid  |
+------+--+
| 2    |
+------+--+

1+------------+-------------------+--------------+--+ 2| score.uid | score.subject_id | score.score | 3+------------+-------------------+--------------+--+ 4| 1 | 1 | 50 | 5| 1 | 2 | 60 | 6| 1 | 3 | 70 | 7| 2 | 1 | 70 | 8| 2 | 2 | 60 | 9| 2 | 3 | 80 | 10| 3 | 1 | 20 | 11| 3 | 2 | 60 | 12| 3 | 3 | 70 | 13+------------+-------------------+--------------+--+

1.先查出平均成绩 t1表

1select subject_id,avg(score)as avgScore 2from score 3group by subject_id; 4+-------------+---------------------+--+ 5| subject_id | avgscore | 6+-------------+---------------------+--+ 7| 1 | 46.666666666666664 | 8| 2 | 60.0 | 9| 3 | 73.33333333333333 | 10+-------------+---------------------+--+

2.拼接到一行 t2表

1select t.uid,t.subject_id,t.score,t1.avgScore from score t 2left join (select subject_id,avg(score)as avgScore from score group by subject_id) t1 3on t.subject_id=t1.subject_id 4; 5+--------+---------------+----------+---------------------+--+ 6| t.uid | t.subject_id | t.score | t1.avgscore | 7+--------+---------------+----------+---------------------+--+ 8| 1 | 1 | 50 | 46.666666666666664 | 9| 1 | 2 | 60 | 60.0 | 10| 1 | 3 | 70 | 73.33333333333333 | 11| 2 | 1 | 70 | 46.666666666666664 | 12| 2 | 2 | 60 | 60.0 | 13| 2 | 3 | 80 | 73.33333333333333 | 14| 3 | 1 | 20 | 46.666666666666664 | 15| 3 | 2 | 60 | 60.0 | 16| 3 | 3 | 70 | 73.33333333333333 | 17+--------+---------------+----------+---------------------+--+

3.查询出偏科同学数据 t3表

select t.uid,t.subject_id,t.score,t1.avgScore from score t 
left join (select subject_id,avg(score)as avgScore from score group by subject_id) t1 
on t.subject_id=t1.subject_id
where t.score< t1.avgScore;

1+--------+---------------+----------+---------------------+--+ 2| t.uid  | t.subject_id  | t.score  |     t1.avgscore     | 3+--------+---------------+----------+---------------------+--+ 4| 1      | 3             | 70       | 73.33333333333333   | 5| 3      | 1             | 20       | 46.666666666666664  | 6| 3      | 3             | 70       | 73.33333333333333   | 7+--------+---------------+----------+---------------------+--+

4.过滤数据取相反逻辑 t4

select t3.uid 
from score t3 
left join 
(select t.uid, t.subject_id,t.score,t1.avgScore from score t left join (select subject_id, avg(score) as avgScore from score group by subject_id)t1 on t.subject_id = t1.subject_id where t.score < t1.avgScore) t2 
on t2.uid = t3.uid
where t2.uid is null;

1+---------+--+ 2| t3.uid | 3+---------+--+ 4| 2 | 5| 2 | 6| 2 | 7+---------+--+

5. 对数据去重获取最终结果

select uid from 
( select t3.uid from score t3 left join ( select t.uid, t.subject_id, t.score, t1.avgScore from score t left join ( select subject_id, avg(score) as avgScore from score group by subject_id)t1 on t.subject_id = t1.subject_id where t.score < t1.avgScore)t2 on t2.uid = t3.uid where t2.uid is null) t4 
group by uid;
 

1##找出所有科目成绩都大于某一学科平均成绩的用户 21. 先查出平均成绩 3select subject_id, avg(score) as avgScore from score group by subject_id; 42. 拼接到一行 5select t.uid, t.subject_id, t.score, t1.avgScore from score t left join (select subject_id, avg(score) as avgScore from score group by subject_id) t1 on t.subject_id = t1.subject_id; 63. 取成绩小于平均成绩的同学数据 7select t.uid, t.subject_id, t.score, t1.avgScore from score t left join (select subject_id, avg(score) as avgScore from score group by subject_id) t1 on t.subject_id = t1.subject_id where t.score < t1.avgScore; 84. 按相反逻辑取数据拿到大于平均成绩的同学 9select t3.uid, t3.subject_id, t3.score from score t3 left join (select t.uid, t.subject_id, t.score, t1.avgScore from score t left join (select subject_id, avg(score) as avgScore from score group by subject_id) t1 on t.subject_id = t1.subject_id where t.score < t1.avgScore) t2 on t2.uid = t3.uid where t2.uid is null; 105. 对数据去重获取最终结果 11select uid,subject_id,score from(select t3.uid, t3.subject_id, t3.score from score t3 left join (select t.uid, t.subject_id, t.score, t1.avgScore from score t left join (select subject_id, avg(score) as avgScore from score group by subject_id) t1 on t.subject_id = t1.subject_id where t.score < t1.avgScore) t2 on t2.uid = t3.uid where t2.uid is null) t4 group by uid,subject_id,score; 12+------+-------------+--------+--+ 13| uid | subject_id | score | 14+------+-------------+--------+--+ 15| 2 | 1 | 70 | 16| 2 | 2 | 60 | 17| 2 | 3 | 80 | 18+------+-------------+--------+--+
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )