思路: ①分组:按照author,月份进行分组; ②各计算值:每月新增粉丝=当月内粉丝变化数之和(注意把2变为-1);总播放量用count即可;累计粉丝新增数用窗口函数sum(每月新增粉丝数)over(partition by author order by 月份),注意要加order by才能逐行累加; ③筛选条件:年份为2021。 题目瑕疵:题中所说截至当前累计粉丝数应该为 2021年总新增粉丝数。因为作者如果2020年及以前就有粉丝累计,那么就需要增加2020年及以前分数数的计算代码,跟题意不符。 select author, date_format(end_time,'%Y-%m') as `month`, round(sum(if(if_follow=2,-1,if_follow))/count(author),3) as fans_growth_rate, sum(sum(if(if_follow=2,-1,if_follow)))over(partition by author order by date_format(end_time,'%Y-%m')) as total_fans from tb_user_video_log left join tb_video_info using(video_id) where year(end_time)=2021 group by author, date_format(end_time,'%Y-%m') order by author, total_fans