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

每个月Top3的周杰伦歌曲

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

with a as(
    select 
        month(fdate)  as month
        ,play_log.user_id user_id
        ,play_log.song_id song_id
        ,song_name
        ,singer_name
        from play_log 
        left join song_info on play_log.song_id = song_info.song_id
        left join user_info on play_log.user_id = user_info.user_id
        where age >=18 and age <= 25
        and singer_name = "周杰伦"
        )

select
month	
,ranking	
,song_name	
,play_pv
from 
(
  select* from(
    select 
    month
    , row_number()over(partition by month order by play_pv desc) ranking
    ,song_id
    ,play_pv
    from 
    ( select 
    month
    ,song_id
    ,count(song_id) play_pv
    from a
    group by month,song_id
    order by month,song_id ) b   
    # 这一层子查询必须加上song_id排序,不然后面排不上
     ) c
    where ranking <= 3
    # over窗口函数ranking加一层子查询才能where
    order by month,ranking
) d left join song_info on d.song_id = song_info.song_id 
   
# 这个题目还有几个盲区:一个用户重复播放算不算多次播放?升高难度:1首歌1个用户播放计1次求排序,或每个用户的TOP3播放
#    select * from song_info where  song_name in ("爱情废柴" ,"伊斯坦堡")

全部评论

相关推荐

07-25 10:31
门头沟学院 Java
求问各位大佬,笔试都考点啥
投递科大讯飞等公司10个岗位
点赞 评论 收藏
分享
07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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