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

每个月Top3的周杰伦歌曲

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

WITH
    month_play AS (
        select
            MONTH (fdate) AS month,
            P.song_id AS id,
            S.song_name AS name,
            count(*) AS cnt
        from
            play_log P
            JOIN user_info U ON P.user_id = U.user_id
            JOIN song_info S ON P.song_id = S.song_id
        WHERE
            U.age BETWEEN 18 AND 25
            AND S.singer_name = '周杰伦'
        group by
            MONTH(fdate),
            P.song_id,
            S.song_name
    ),

ranked_song AS (
    SELECT *, 
        ROW_NUMBER() OVER (PARTITION BY month
                        ORDER BY cnt DESC, id ) AS rnk
    FROM month_play
)

SELECT month, rnk AS ranking, name AS song_name, cnt AS play_pv
FROM ranked_song
WHERE rnk <=3;

全部评论

相关推荐

2025-12-18 20:31
湖南大学 算法工程师
饿魔:没人说?我来牛美孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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