希望对用户群进行一些群组分析。我们有2个表"用户"(美国)和"会话"(s),其中用户有" created_at"字段和会话具有“start_at”字段。
基本上我正在寻找的是查看在特定月份注册了多少用户,并分析了其中有多少用户在接下来的几个月内回归。
澄清我想要的内容,以防我不清楚我的解释。
Month: January
Registered users in January: 100
How many of this 100 registered users, logged in in February?: 97
How many of this 100 registered users, logged in in Mars?: 56
Month: February
Registered users in February: 70
How many of this 70 registered users, logged in in Mars?: 10
How many of this 70 registered users, logged in in April?: 32
等等......
我正在使用以下查询代码,请记住我的表日期是UNIX时间戳格式,这就是我使用from_unixtime()公式的原因。
select
Month(from_unixtime(up.registered_at)) as Month,
count(distinct up.id) registered,
count(DISTINCT (CASE WHEN datediff(from_unixtime(u.registered_at),from_unixtime(s.start_at)) <= 60 AND datediff(from_unixtime(u.registered_at),from_unixtime(s.start_at)) > 30 THEN u.id END)) as 30to60,
count(DISTINCT (CASE WHEN datediff(from_unixtime(u.registered_at),from_unixtime(s.start_at)) <= 90 AND datediff(from_unixtime(u.registered_at),from_unixtime(s.start_at)) > 60 THEN u.id END)) as 60to90
from users u
left join sessions s
on u.id=s.user_id
group by 1
limit 100
该查询给了我不正确的数据,它确切地说明了已注册人数,但没有多少人在接下来的几个月内回来。
请你帮我一把,我相信这比我想象的要容易。
提前谢谢。
答案 0 :(得分:1)
我会想到这样的事情:
select date_format(from_unixtime(up.registered_at), '%Y-%m') as reg_yyyymm,
date_format(from_unixtime(s.start_at), '%Y-%m') as sess_yyyymm,
count(distinct u.id)
from users u left join
sessions s
on u.id = s.user_id
group by reg_yyyymm, sess_yyyymm;
这似乎给出了您描述的结果。