mysql – 根据登录计算活动用户最近90天
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mysql – 根据登录计算活动用户最近90天,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3257字,纯文字阅读大概需要5分钟。
内容图文
![mysql – 根据登录计算活动用户最近90天](/upload/InfoBanner/zyjiaocheng/894/5e818e6d63854a08bf4a4df0d86fdd39.jpg)
我正在尝试进行一个查询,该查询将显示一个列表,显示我们每月有多少活跃用户.我们将活跃用户定义为在过去90天内登录的用户.
我可以通过它轻松定义我们现在拥有的活跃用户数
SELECT COUNT(DISTINCT(user_id) FROM login_table
WHERE login_date BETWEEN DATE_SUB(login_date, INTERVAL 90 DAY) AND NOW())
当我必须逐月计算每月用户数量时,我的问题就来了
在这里,我必须多次计算一次登录.
如果我的用户在1月10日登录,并且再也没有登录,则该用户在以下几个月内应计为活跃用户:1月,2月,3月和4月,即使我只有一个用户注册
示例数据:
login_date | user_id
2015-01-01 | 1
2015-02-10 | 1
2015-02-11 | 2
2015-02-13 | 1
2015-03-19 | 1
这应该导致这样的事情:
Date | Active users
2015-01 | 1
2015-02 | 2
2015-03 | 2
2015-04 | 2
2015-05 | 2
2015-06 | 1
2015-07 | 0
2015-08 | 0
无论如何做这样的计数?
解决方法:
如果您有一个包含所需日期的日历表,那么这样的问题就更容易解决了.如果您没有这样的表,可以使用如下查询创建它:
create table `calendar` (
`date` DATE NOT NULL,
PRIMARY KEY (`date`)
)
select DATE_ADD('1900-01-01',INTERVAL t4.c*10000 + t3.c*1000 + t2.c*100 + t1.c*10 + t0.c DAY) as `date`
from
(select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t0,
(select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t1,
(select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3,
(select 0 c union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4
这将创建一个日期从1900-01-01到2173-10-15(100K天)的表,并且只消耗大约2.5 MB.您可以根据自己的需要进行调整.
使用caledar表,您可以获得三个月的范围:
select
DATE_FORMAT(date_sub(c.date, INTERVAL 1 day), '%Y-%m') as month,
date_sub(c.date, INTERVAL 3 month) as first_day,
date_sub(c.date, INTERVAL 1 day) as last_day
from calendar c
where day(c.date) = 1
and c.date between '2015-02-01' and '2015-09-01'
结果:
| month | first_day | last_day |
| 2015-01 | 2014-11-01 | 2015-01-31 |
| 2015-02 | 2014-12-01 | 2015-02-28 |
| 2015-03 | 2015-01-01 | 2015-03-31 |
| 2015-04 | 2015-02-01 | 2015-04-30 |
| 2015-05 | 2015-03-01 | 2015-05-31 |
| 2015-06 | 2015-04-01 | 2015-06-30 |
| 2015-07 | 2015-05-01 | 2015-07-31 |
| 2015-08 | 2015-06-01 | 2015-08-31 |
调整它,如果你真的想要使用90天的间隔.
现在,这是一个简单的左连接与登录表,以获得您想要的:
select i.month as `Date`, count(distinct l.user_id) as `Active users`
from (
select
date_format(date_sub(c.date, interval 1 day), '%Y-%m') as month,
date_sub(c.date, interval 3 month) as first_day,
date_sub(c.date, interval 1 day) as last_day
from calendar c
where day(c.date) = 1
and c.date between '2015-02-01' and '2015-09-01'
) i
left join login_table l on l.login_date between i.first_day and i.last_day
group by i.month
http://sqlfiddle.com/#!9/d1bb0/3
内容总结
以上是互联网集市为您收集整理的mysql – 根据登录计算活动用户最近90天全部内容,希望文章能够帮你解决mysql – 根据登录计算活动用户最近90天所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。