题目中
#问题:计算2021年里每个创作者每月的涨粉率及截止当月的总粉丝量
#注:
#涨粉率=(加粉量 - 掉粉量) / 播放量。结果按创作者ID、总粉丝量升序排序。
#if_follow-是否关注为1表示用户观看视频中关注了视频创作者,为0表示此次互动前后关注状态未发生变化,为2表示本次观看过程中取消了关注。
SELECT
author,
DATE_FORMAT(start_time, '%Y-%m') month,
ROUND((COUNT(IF(if_follow = 1, 1, NULL)) - COUNT(IF(if_follow = 2, 1, NULL))) / COUNT(start_time), 3) fans_growth_rate,
sum(sum(case when if_follow = 1 then 1 when if_follow=2 then -1 end)) over(partition by author order by DATE_FORMAT(start_time, '%Y-%m')) total_fans
FROM
_21_tb_user_video_log
RIGHT JOIN _21_tb_video_info USING (video_id)
WHERE
YEAR(start_time) = 2021
GROUP BY author , month
ORDER BY author , total_fans;