【问题标题】:To find all the active users in a bucket of 15 days in SQL在 SQL 中查找 15 天的存储桶中的所有活跃用户
【发布时间】:2015-05-07 10:27:13
【问题描述】:

我有两张桌子:usersuser_source_history

users表中保存了所有已注册的用户。并且,在表user_source_history 中,存储了他们的活动。这些活动可能包括登录系统、订购东西。所以一个用户可以在user_source_history 中多次存在。

现在的问题是我需要在 15 天的存储桶中找到所有活跃用户。

因此,例如,用户在 2014 年 12 月 3 日购买了一些东西。现在,该用户将在 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

如何拆分查询以运行多行?

【问题讨论】:

    标签: mysql sql nested-queries


    【解决方案1】:

    这是查找从 '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
    

    【讨论】:

      【解决方案2】:

      下面是一个查询过去 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
      

      【讨论】:

      • 我试过这个,但这个查询从现在开始为我提供活跃用户的用户。但我想为所有活跃用户/每天的日期范围找到活跃用户
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      相关资源
      最近更新 更多