题解 | 每个月Top3的周杰伦歌曲

每个月Top3的周杰伦歌曲

https://www.nowcoder.com/practice/4ab6d198ea8447fe9b6a1cad1f671503

思路:
1.首先通过子查询过滤数据:(1)找出符合年龄条件的user_id,(2)符合所求歌手的song_id,(3)年份在2022年
2.查找top3歌曲:使用row_number()进行排序。可能存在并列的情况,这里设定并列时按song_id大小升序选取。
3.join操作放在最后:join歌曲信息表,加上歌曲名称。
select
    t3.mon as month,
    t3.ranking,
    si.song_name,
    t3.play_pv
from(
    select
        song_id,
        mon,
        ranking,
        play_pv
    from
        (select
            mon,
            song_id,
            play_pv,
            row_number() over(partition by mon order by play_pv desc, song_id asc) as ranking
        from(select
                month(fdate) as mon,
                song_id,
                count(*) as play_pv
            from play_log
            where user_id in (select user_id from user_info where age between 18 and 25) and year(fdate) = 2022
            and song_id in (select song_id from song_info where singer_name in ("周杰伦"))
            group by mon, song_id)t1
        )t2
    where ranking <= 3
)t3
left join song_info si
on si.song_id = t3.song_id

全部评论

相关推荐

05-20 10:33
东南大学
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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