select a.author,DATE_FORMAT(b.start_time,'%Y-%m'),-- count(b.if_follow) 播放人数, -- sum(case when b.if_follow=2 then 1 else 0 end) 掉粉人数, ROUND((sum(case when b.if_follow=1 then 1 else 0 end)-sum(case when b.if_follow=2 then 1 else 0 end))/count(b.if_follow),3) 涨粉率, sum(sum(case when b.if_follow=1 then 1 when b.if_follow=2 then -1 else 0 end))over(partition by a.author order by DATE_FORMAT(b.start_time,'%Y-%m')) 涨粉人数 from tb_user_video_log b left join tb_video_info a on a.video_id=b.video_id where year(b.start_time)='2021' group by a.author,DATE_FORMAT(b.start_time,'%Y-%m') order by a.author,涨粉人数
难点:截至当月的粉丝数
使用窗口函数累加粉丝数sum() over+case when
还修改了时间格式date_format