题解 | 每个月Top3的周杰伦歌曲
每个月Top3的周杰伦歌曲
https://www.nowcoder.com/practice/4ab6d198ea8447fe9b6a1cad1f671503
-- 题目忘了说明相同play_pv, 按song_id升序。 -- 如果提交报错,可能是因为存在排名听歌数量相同但是排名不一样的歌曲,因此需要在窗口函数的order by部分按照song_id再升序排列即可. -- 思路: -- 连接表 -- 筛选条件年份、年龄、周杰伦 -- group by song_id, month,song_id(因为窗口函数中用到song_id排序。所以分组时要加上song_id) -- count(*) -- row_number()over(partition by month(date) order by cnt) with t1 as( select month(fdate) month ,row_number()over(partition by month(fdate) order by count(*)desc, p.song_id) ranking ,song_name ,count(*) play_pv from play_log p left join song_info s on p.song_id = s.song_id left join user_info i on p.user_id = i.user_id where year(fdate) = 2022 and age between 18 and 25 and singer_name = '周杰伦' group by song_name,month,p.song_id ) select month ,ranking ,song_name ,play_pv from t1 where ranking <= 3