Mysql:查询月流失用户数--紧急求解

需求:查询指定月份的流失用户数_
_

原表:f_user
图片说明

_

预期返回结果:
图片说明

_

user_id :用户唯一标识_

created_at 账号注册创建日期

update_at 最近一次登录日期

_

流失用户定义: 超过1个月未登录即为流失

下面的a表你应该有的。或者你应该做一个连续月份的临时表。流失默认是到月底的。就是当月没有登录算流失

select a.yf,
count(case when substr(created_at,1,7)<=yf then id else null end) zongshu_ct,
count(case when substr(created_at,1,7)=yf then id else null end) xinzeng_ct,
count(case when substr(update_at,1,7)<yf then id else null end) liushi_ct

from (
select '2019-10' yf
union all 
select '2019-11' yf
union all 
select '2019-12' yf
union all 
select '2020-01' yf
union all 
select '2020-02' yf
union all 
select '2020-03' yf
) a 
join f_user 
on 1=1
group by a.yf

楼上的 count(case when substr(update_at,1,7)<yf then id else null end) liushi_ct
这行是累计流失用户 不是当月流失,需要将 '<yf '改为 '= yf 减一个月' 其他挺好