题解 | #每个创作者每月的涨粉率及截止当前的总粉丝量#

每个创作者每月的涨粉率及截止当前的总粉丝量

https://www.nowcoder.com/practice/d337c95650f640cca29c85201aecff84

select
    author,
    month,
    round(fans_growth_rate,3),
    sum(rollnum) over (
        partition by
            author
        order by
            month
    ) total_fans
from
    (
        select
            author,
            date_format (start_time, '%Y-%m') month,
            sum(if (if_follow = 2, -1, if_follow)) / count(1)     fans_growth_rate,
            sum(if (if_follow = 2, -1, if_follow)) as rollnum
        from
            tb_user_video_log
            join tb_video_info using (video_id)
            where year(start_time) = 2021
        group by
            author,
            month
    ) t1
order by  author ,total_fans

本题难度在sql进阶知识点中已经有提及,在sum()函数中增加over()窗口函数 partition by author order by month就可以计算累计月份的粉丝数量了。

全部评论

相关推荐

代码飞升:简历差不多情况下你的学历已经加分了,海投就行,加油,不要追求尽善尽美
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务