猿辅导数据分析一面sql题 我太难了

表名: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)       |              |              |              |

|
+---------------+--------------+--------------+--------------

+--------------+

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;




#数据分析##笔试题目##猿辅导#
全部评论
楼主能请问一下你是几号笔试的吗
点赞 回复
分享
发布于 2020-08-27 14:04
sql第一题楼主自己想的那种方法select的字段里不能有course_id~
点赞 回复
分享
发布于 2020-08-29 17:16
小红书
校招火热招聘中
官网直投
楼主sql水平仍需提高 1) select studentid,courseid,score from (select *,rank() over(partition by studentid order by score) rk from course_student_score_di)T where rk=1 2) select studentid,max(case when courseid='语文&(8803)#39; then score end) '语文&#39;                            max(case when courseid='数学&(8804)#39; then score end) '数学&#39;                            max(case when courseid='英语&(8805)#39; then score end) '英语&#39;                            max(case when courseid='物理&(10629)#39; then score end) '物理&#39; from course_student_score_di group by studentid 第二题还可以用pivot函数
点赞 回复
分享
发布于 2020-10-25 10:47

相关推荐

投递美团等公司10个岗位
点赞 评论 收藏
转发
3 58 评论
分享
牛客网
牛客企业服务