题解 | 窗口函数+子查询

查询每天刷题通过数最多的前二名用户id和刷题数

https://www.nowcoder.com/practice/b9cc0d5047f94bc0a661c5a0a230b9cd

题目需求:

查询【每天刷题通过数最多的】【前两名用户】user_id和刷题数

思路:

1.先用窗口函数row_number() 按照每天刷题数目通过数(pass_count)新增一列排序列count

2.再通过子查询,以及where语句,筛选出count列≤2(前两名)的用户

3.order by 排序

代码:

select date,user_id,pass_count
from
(select date,user_id,pass_count,row_number() over(partition by date order by pass_count DESC) as count
from questions_pass_record)m
where count<=2
order by date;

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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