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

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

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

方法1:用子循环,因为device与gpa对应,所以不能直接用聚合函数,考虑使用子循环.让学校与min(gpa)一一对应

select device_id,university,gpa from user_profile
    where (gpa,university) in(select min(gpa),university from user_profile
            group by university)
order by university

方法2:使用窗口函数,先对每个学校的成绩进行排序,再选出最低的,注意sql中的窗口函数不能直接命名为rank

    select device_id,university,gpa,
     row_number()over(partition by university order by gpa) as t_rank
    from user_profile
)t
where t_rank=1
order by university
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务