mysql统计用户周活跃_MySql查询脚本用于计算每个月的活跃用户数

你可以用一招来做到这一点.从每个日期添加一个月,然后您可以使用union all和聚合计数:

select year(logindate), month(logindate), count(distinct userid)

from ((select logindate, userid

from logger

) union all

(select date_add(longdate, interval 1 month), userid

from logger

)

) l

group by year(logindate), month(logindate)

order by 1, 2;

编辑:

哦,我误解了这个问题.您需要连续两个月才能成为活跃用户.我知道用户登录会让用户活跃两个月.好的,您可以通过加入或存在来解决此问题:

select year(l.logindate), month(l.logindate), count(distinct l.userid)

from logger l

where exists (select 1

from logger l2

where l2.userid = l.userid and

year(date_sub(l.logindate, interval 1 month)) = year(l2.logindate) and

month(date_sub(l.logindate, interval 1 month)) = month(l2.logindate)

)

group by year(l.logindate), month(l.logindate);


版权声明:本文为weixin_39541600原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。