查找 SQL 中 15 天的所有活跃用户
To find all the active users in a bucket of 15 days in SQL
我有两个 table:users
、user_source_history
。
在tableusers
中,保存了所有注册过的用户。并且,在 table user_source_history
中,存储了他们的活动。这些活动可能包括登录系统、订购东西。所以一个用户可以在 user_source_history
中存在多次。
现在的问题是我需要在 15 天的存储桶中找到所有活跃用户。
所以,例如,用户在 2014-12-03 购买了东西。现在,该用户将显示为活跃用户,直到 2014-12-18。
我需要找到日期范围内的所有活跃用户。
因此,例如,在这个日期范围内-
2014-12-03 - 10 users are active.
2014-12-04 - 10(active on 2014-12-03) + (new users for this date)
2014-12-05 - Number on active user on this date + all users in 15 day bucket
2014-12-06 - Number on active user on this date + all users in 15 day bucket
查询:
SELECT CAST(`user_last_interaction_date` as Date)
FROM `users` U
LEFT JOIN `user_source_history` USH on U.user_id = USH.user_id
WHERE `user_last_interaction_date` >
(SELECT DATE_ADD(`user_last_interaction_date`, INTERVAL -15 DAY)
FROM `users`
GROUP BY CAST(`user_last_interaction_date` as Date)
)
GROUP BY CAST(`user_last_interaction_date` as Date)
我试过这个查询,但是 SQL 说 Subquery returns more than one row
如何将查询拆分为 运行 多行?
下面是一个查询在过去 15 天内活跃的用户的示例:
select distinct name
from users u
join user_source_history uh
on uh.user_id = u.user_id
where user_last_interaction_date between
now() and now() - interval 15 day
这是查找从“2015-05-03”到“2015-05-18”期间没有活跃用户的查询
您需要使用 calendar_date 生成所有日期,它可以是您数据库架构中的日期字段。
SELECT c.calendar_date, count(*) active_users
FROM `users` U
LEFT JOIN `user_source_history` USH
on( U.user_id = USH.user_id )
CROSS JOIN (select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) `calendar_date` from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where calendar_date between '2015-05-03' and '2015-05-18') as c
on
(date(USH.user_last_interaction_date) < c.calendar_date
and date(USH.user_last_interaction_date) >= (SELECT DATE_ADD(c.calendar_date, INTERVAL -15 DAY)))
group by c.calendar_date
我有两个 table:users
、user_source_history
。
在tableusers
中,保存了所有注册过的用户。并且,在 table user_source_history
中,存储了他们的活动。这些活动可能包括登录系统、订购东西。所以一个用户可以在 user_source_history
中存在多次。
现在的问题是我需要在 15 天的存储桶中找到所有活跃用户。
所以,例如,用户在 2014-12-03 购买了东西。现在,该用户将显示为活跃用户,直到 2014-12-18。
我需要找到日期范围内的所有活跃用户。
因此,例如,在这个日期范围内-
2014-12-03 - 10 users are active.
2014-12-04 - 10(active on 2014-12-03) + (new users for this date)
2014-12-05 - Number on active user on this date + all users in 15 day bucket
2014-12-06 - Number on active user on this date + all users in 15 day bucket
查询:
SELECT CAST(`user_last_interaction_date` as Date)
FROM `users` U
LEFT JOIN `user_source_history` USH on U.user_id = USH.user_id
WHERE `user_last_interaction_date` >
(SELECT DATE_ADD(`user_last_interaction_date`, INTERVAL -15 DAY)
FROM `users`
GROUP BY CAST(`user_last_interaction_date` as Date)
)
GROUP BY CAST(`user_last_interaction_date` as Date)
我试过这个查询,但是 SQL 说 Subquery returns more than one row
如何将查询拆分为 运行 多行?
下面是一个查询在过去 15 天内活跃的用户的示例:
select distinct name
from users u
join user_source_history uh
on uh.user_id = u.user_id
where user_last_interaction_date between
now() and now() - interval 15 day
这是查找从“2015-05-03”到“2015-05-18”期间没有活跃用户的查询
您需要使用 calendar_date 生成所有日期,它可以是您数据库架构中的日期字段。
SELECT c.calendar_date, count(*) active_users
FROM `users` U
LEFT JOIN `user_source_history` USH
on( U.user_id = USH.user_id )
CROSS JOIN (select * from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) `calendar_date` from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where calendar_date between '2015-05-03' and '2015-05-18') as c
on
(date(USH.user_last_interaction_date) < c.calendar_date
and date(USH.user_last_interaction_date) >= (SELECT DATE_ADD(c.calendar_date, INTERVAL -15 DAY)))
group by c.calendar_date