SQL 90 ) 获得积分最多的人(二)

获得积分最多的人(二)

http://www.nowcoder.com/questionTerminal/b6248d075d2d4213948b2e768080dc92

使用排序函数rank()over()嵌套sum()over()解决

select u.id, u.name , a.grade
from
(select user_id , g.grade , rank()over(order by g.grade desc) r_number
from
(select user_id, sum(grade_num)over(partition by user_id) grade
from grade_info)g
group by user_id)a
join
user as u
on a.user_id = u.id
where a.r_number = 1
order by u.id asc;
SQL 文章被收录于专栏

SQL

全部评论
把子查询里的group by删掉就好了
2 回复 分享
发布于 2021-11-15 09:42
为什么在mysql中不能通过呢,在sqlite就可以
点赞 回复 分享
发布于 2021-11-30 21:08
select distinct u.id, u.name, a.grade from (select user_id, g.grade, rank()over(order by g.grade desc) r_number from (select user_id, sum(grade_num)over(partition by user_id) grade from grade_info) g) a join user as u on a.user_id = u.id where a.r_number = 1 order by u.id group by去掉就好了
点赞 回复 分享
发布于 2023-12-12 18:17 北京
select distinct u.id,u.name,t.grade_num from user u , (select *,dense_rank()over(order by grade_num desc)as rn from (select user_id,sum(grade_num)over(partition by user_id ) as grade_num from grade_info) a) t where u.id=t.user_id and rn =1 order by u.id;
点赞 回复 分享
发布于 07-26 12:06 江苏

相关推荐

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