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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值