题解 | #找出每个学校GPA最低的同学#

找出每个学校GPA最低的同学

https://www.nowcoder.com/practice/90778f5ab7d64d35a40dc1095ff79065

# # with子句 + 一步步拆解/
# with
#     temp as(
#         #对每个学校的gpa排序
#         select distinct
#             device_id,
#             university,
#             row_number() over(partition by university order by gpa) as ranking,
#             min(gpa) over(partition by university order by gpa) as gpa
#         from user_profile
#         order by university
#     )
# #取出每个学校gpa最低的同学的device_id
# select 
#     device_id,
#     university,
#     gpa
# from temp
# where ranking = 1

# # 表连接
# select
#     u.device_id,
#     u.university,
#     u.gpa
# from user_profile u
# join(
#     #统计每个学校的最低gpa
#     select
#         university,
#         min(gpa) as gpa
#     from user_profile
#     group by university
#     ) as attr on u.university = attr.university and u.gpa = attr.gpa
# order by university

# # where子句
# select
#     device_id,
#     university,
#     gpa
# from user_profile
# where (university,gpa) in(
#     #统计每个学校的最低gpa
#     select
#         university,
#         min(gpa) as gpa
#     from user_profile
#     group by university
# )
# order by university


select device_id,university,gpa
from
(select *,rank() over (partition by university order by gpa) as rk 
from user_profile) a
where a.rk=1
order by university;

"(select *,rank() over (partition by university order by gpa) as rk

from user_profile)" 为临时创建的派生表,后面必须附带有唯一标识的表名,例如 a;

rank()/row_number()/dense_rank()/min()/sum()... over(partition by xx order by xxx)

全部评论

相关推荐

我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务