表名:course_student_score_di
表数据(例):(每个学生在该科目下仅有一个分数)
+---------------+--------------+--------------+
| studentid | courseid | score |
+---------------+--------------+--------------+
| aa | 语文 | 90 |
| aa | 数学 | 100 |
| aa | 英语 | 90 |
| aa | 物理 | |
| bb | 语文 | 47 |
| bb | 数学 | 59 |
| bb | 英语 | |
| bb | 物理 | 80 |
| cc | 语文 | 87 |
| cc | 数学 | 83 |
| cc | 英语 | 68 |
| dd | 语文 | 74 |
| dd | 数学 | 82 |
| dd | 英语 | 95 |
| dd | 物理 | 89 |
+---------------+--------------+--------------+
注:每个学生在该科目下仅有一个分数
问题:
1)求每个学生分数最高的科目及对应分数,输出结构为三列
(studentid,courseid,score,若两科目均为分数最高科目则都输出)
2)用SQL实现下面表结构(每个科目下如下分数段的学生人数)的查询结果
+---------------+--------------+--------------+--------------
+--------------+
| 分数段 | 语文 | 数学 | 英语 | 物理
|
+---------------+--------------+--------------+--------------
+--------------+
| [0,60) | | | |
|
| [60,80) | | | |
|
+---------------+--------------+--------------+--------------
+--------------+
表数据(例):(每个学生在该科目下仅有一个分数)
+---------------+--------------+--------------+
| studentid | courseid | score |
+---------------+--------------+--------------+
| aa | 语文 | 90 |
| aa | 数学 | 100 |
| aa | 英语 | 90 |
| aa | 物理 | |
| bb | 语文 | 47 |
| bb | 数学 | 59 |
| bb | 英语 | |
| bb | 物理 | 80 |
| cc | 语文 | 87 |
| cc | 数学 | 83 |
| cc | 英语 | 68 |
| dd | 语文 | 74 |
| dd | 数学 | 82 |
| dd | 英语 | 95 |
| dd | 物理 | 89 |
+---------------+--------------+--------------+
注:每个学生在该科目下仅有一个分数
问题:
1)求每个学生分数最高的科目及对应分数,输出结构为三列
(studentid,courseid,score,若两科目均为分数最高科目则都输出)
2)用SQL实现下面表结构(每个科目下如下分数段的学生人数)的查询结果
+---------------+--------------+--------------+--------------
+--------------+
| 分数段 | 语文 | 数学 | 英语 | 物理
|
+---------------+--------------+--------------+--------------
+--------------+
| [0,60) | | | |
|
| [60,80) | | | |
|
+---------------+--------------+--------------+--------------
+--------------+
1)
select studentid, courseid,score from( select studentid, courseid,score, dense_rank() over(partition by studentid order by score desc) as rank from course_student_score_di where rank=1)
select a.studentid, a.courseid,a.score from course_student_score_di a,course_student_score_di b where a.studentid=b.studentid and a.score<=b.score group by a.studentid having count(distinct b.score)=1
select studentid, courseid,score from course_student_score_di group by studentid having max(score);上面俩是刚刚想的....最后一个是当时面试的时候写的,最后一个是不是输出不了一样大的?求大佬给看看....
2)这个当时没写出来.....我太难了
select '[0,60)' as ‘分数段’ , sum(case when courseid='语文' and score<60 then 1 else 0 end) as '语文', sum(case when courseid='数学' and score<60 then 1 else 0 end) as '数学', sum(case when courseid='英语' and score<60 then 1 else 0 end) as '英语', sum(case when courseid='物理' and score<60 then 1 else 0 end) as '物理' from course_student_score_di union select '[60,80)' as ‘分数段’ , sum(case when courseid='语文' and score<80 and score>=60 then 1 else 0 end) as '语文', sum(case when courseid='数学' and score<80 and score>=60 then 1 else 0 end) as '数学', sum(case when courseid='英语' and score<80 and score>=60 then 1 else 0 end) as '英语', sum(case when courseid='物理' and score<80 and score>=60 then 1 else 0 end) as '物理' from course_student_score_di;
全部评论
(3) 回帖