HQL練習1--找出全部科目成績都大於某一學科平均成績的用戶

表結構: uid,subject_id,score
求: 找出全部科目成績都大於某一學科平均成績的用戶ui

建表語句spa

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

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

+------------+-------------------+--------------+--+
| score.uid  | score.subject_id  | score.score  |
+------------+-------------------+--------------+--+
| 1          | 1                 | 50           |
| 1          | 2                 | 60           |
| 1          | 3                 | 70           |
| 2          | 1                 | 70           |
| 2          | 2                 | 60           |
| 2          | 3                 | 80           |
| 3          | 1                 | 20           |
| 3          | 2                 | 60           |
| 3          | 3                 | 70           |
+------------+-------------------+--------------+--+

1.先查出平均成績 t1表orm

select subject_id,avg(score)as avgScore
from score
group by subject_id;
+-------------+---------------------+--+
| subject_id  |      avgscore       |
+-------------+---------------------+--+
| 1           | 46.666666666666664  |
| 2           | 60.0                |
| 3           | 73.33333333333333   |
+-------------+---------------------+--+

2.拼接到一行 t2表string

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
;
+--------+---------------+----------+---------------------+--+
| t.uid  | t.subject_id  | t.score  |     t1.avgscore     |
+--------+---------------+----------+---------------------+--+
| 1      | 1             | 50       | 46.666666666666664  |
| 1      | 2             | 60       | 60.0                |
| 1      | 3             | 70       | 73.33333333333333   |
| 2      | 1             | 70       | 46.666666666666664  |
| 2      | 2             | 60       | 60.0                |
| 2      | 3             | 80       | 73.33333333333333   |
| 3      | 1             | 20       | 46.666666666666664  |
| 3      | 2             | 60       | 60.0                |
| 3      | 3             | 70       | 73.33333333333333   |
+--------+---------------+----------+---------------------+--+

 3.查詢出偏科同窗數據 t3表it

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;table

+--------+---------------+----------+---------------------+--+
| t.uid  | t.subject_id  | t.score  |     t1.avgscore     |
+--------+---------------+----------+---------------------+--+
| 1      | 3             | 70       | 73.33333333333333   |
| 3      | 1             | 20       | 46.666666666666664  |
| 3      | 3             | 70       | 73.33333333333333   |
+--------+---------------+----------+---------------------+--+

4.過濾數據取相反邏輯 t4form

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;class

+---------+--+
| t3.uid  |
+---------+--+
| 2       |
| 2       |
| 2       |
+---------+--+

5. 對數據去重獲取最終結果select

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. 先查出平均成績
select subject_id, avg(score) as avgScore from score group by subject_id;
2. 拼接到一行
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;
3. 取成績小於平均成績的同窗數據
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;
4. 按相反邏輯取數據拿到大於平均成績的同窗
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;
5. 對數據去重獲取最終結果
select 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;
+------+-------------+--------+--+
| uid  | subject_id  | score  |
+------+-------------+--------+--+
| 2    | 1           | 70     |
| 2    | 2           | 60     |
| 2    | 3           | 80     |
+------+-------------+--------+--+
相關文章
相關標籤/搜索