题解 | #获得积分最多的人(一)#

获得积分最多的人(一)

https://www.nowcoder.com/practice/1bfe3870034e4efeb4b4aa6711316c3b?tpId=82&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%3Fpage%3D2%26tab%3DSQL%25E7%25AF%2587%26topicId%3D82

简单的求最大值的相关信息,被我绕啊绕的,记录一下

我的方法:

with tmp as(
select user_id,name,sum(grade_num)sg from grade_info g join user on g.user_id=user.id
group by user_id,name
)
select name,sg from tmp where sg=(select max(sg)from tmp)

改进:不用group by,用sum窗口函数,取最大值用降序+limit 1

一个select搞定

select name,sum(grade_num)over(partition by user_id)sg from grade_info g join user on g.user_id=user.id
order by sg desc limit 1
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务