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

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

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

select
    *
from
    (
        select
            author,
            month,
            fans_growth_rate,
            sum(fans) over (
                partition by
                    author
                order by
                    month
            ) as total_fans
        from
            (
                select
                    author,
                    month,
                    round(
                        sum(
                            case
                                when if_follow = 1 then 1
                                when if_follow = 2 then -1
                                else 0
                            end
                        ) / count(1),
                        3
                    ) as fans_growth_rate,
                    sum(
                        case
                            when if_follow = 1 then 1
                            when if_follow = 2 then -1
                            else 0
                        end
                    ) as fans
                from
                    (
                        select
                            video_id,
                            date_format (start_time, '%Y-%m') as month,
                            if_follow,
                            author
                        from
                            tb_user_video_log t1
                            inner join tb_video_info t2 using (video_id)
                    ) t3
                group by
                    author,
                    month
            ) t4
    ) t5
where
    total_fans > 0
order by
    author,
    total_fans

全部评论

相关推荐

10-09 09:39
门头沟学院 C++
HHHHaos:这也太虚了,工资就一半是真的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务