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

每个月Top3的周杰伦歌曲

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

-- 使用 with 子句来提高可读性和性能
with t as (
    select
        month(p.fdate) as month,  -- 提取月份
        row_number() over (partition by month(p.fdate) order by count(s.song_name) desc, p.song_id asc) as ranking,  -- 计算排名
        s.song_name,  -- 歌曲名称
        count(s.song_name) as play_pv  -- 播放次数
    from play_log p
    left join song_info s on p.song_id = s.song_id  -- 连接歌曲信息表
    left join user_info u on p.user_id = u.user_id  -- 连接用户信息表
    where 
        u.age between 18 and 25  -- 年龄在18到25岁之间
        and s.singer_name = '周杰伦'  -- 歌手是周杰伦
        and year(p.fdate) = '2022'  -- 日期在2022年
    group by month(p.fdate), s.song_name, p.song_id  -- 按月份、歌曲名称和歌曲ID分组
)

-- 选择排名前3的记录
select 
    month,  -- 月份
    ranking,  -- 排名
    song_name,  -- 歌曲名称
    play_pv  -- 播放次数
from t
where ranking <= 3;  -- 只选择排名前3的记录

全部评论

相关推荐

2025-11-17 11:50
已编辑
西南石油大学 数据分析师
菜鸡oider:兄弟,你这个我说实话,我要不是看你上面写的计算机专业,都不知道你要找什么工作
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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