【问题标题】:MYSQL 'SELECT' and COUNT "active" user for day in user event tableMYSQL 'SELECT' 和 COUNT 用户事件表中一天的“活动”用户
【发布时间】:2014-01-22 13:36:20
【问题描述】:
+----+--------------+------------+--------+
| id | user    | date(data) | type        |
+----+--------------+------------+--------+
|  3 | 5458848 | 2013-12-19 | SUBSCRIBE   |
|  4 | 5458848 | 2013-12-19 | UNSUBSCRIBE |
|  5 | 5458848 | 2013-12-20 | SUBSCRIBE   |
|  7 | 5458848 | 2013-12-20 | UNSUBSCRIBE |
|  8 | 7883870 | 2013-12-20 | SUBSCRIBE   |
|  9 | 7883870 | 2013-12-23 | UNSUBSCRIBE |
| 10 | 7883870 | 2013-12-24 | SUBSCRIBE   |
| 11 | 7883870 | 2013-12-24 | UNSUBSCRIBE |
| 12 | 7883870 | 2013-12-24 | SUBSCRIBE   |
+----+---------+------------+-------------+                                                 

你好,我想知道如何在mysql中查询每天有多少用户活跃和不活跃,

例如:如果一个用户在同一天订阅和取消订阅,这意味着在那一天,我有一个用户不活跃

+------------+-------------+-------------+
| date(data) | active      | inactive    |
+------------+-------------+-------------+
| 2013-12-19 | 0           | 1           |
| 2013-12-20 | 1           | 1           |
| 2013-12-23 | 0           | 2           |
| 2013-12-24 | 1           | 1           |
+------------+-------------+-------------+

【问题讨论】:

    标签: php mysql sql date


    【解决方案1】:
    set @ac:=null;
    DROP TABLE IF EXISTS ___active_nums;
    
    CREATE TABLE ___active_nums(
      event_id integer,
      event_user_id integer,
      event_date  Date,
      number_of_active_users integer,
      INDEX USING BTREE (event_date)
    )
    ENGINE=MEMORY 
    ;
    
    INSERT INTO ___active_nums
    
    SELECT
    
      events.id,
      events.user,
      events.date,
      ifnull( @ac:= ( @ac + if( type='SUBSCRIBE', 1, -1) ), @ac:=if( type='SUBSCRIBE', 1, -1) ) AS active_num
    
    FROM 
    (
      SELECT * FROM events ORDER BY id
    )
    AS events
    ;
    
    SELECT
    
    event_date as date,
    number_of_active_users,
    
    ( SELECT count(DISTINCT event_user_id) FROM ___active_nums WHERE ___active_nums.event_date <= outer_active_nums.event_date )
    - number_of_active_users
    as number_of_inactive_users
    
    FROM ___active_nums  AS outer_active_nums
    
    WHERE event_id IN ( SELECT max(event_id) FROM ___active_nums GROUP BY event_date );
    

    【讨论】:

    • 它不能正常工作,因为在 2013 年 12 月 23 日它给了 1 个活跃用户和 0 个不活跃用户
    • 现在给 0 个活跃用户和 1 个不活跃用户,但不活跃用户是 2.. 在 2013-12-23 我需要看到用户 5458848 + 7883870 不活跃
    • 再次更新了我的答案。它有点长而且丑陋,但它确实有效。希望对你有帮助
    • 在 2013 年 12 月 23 日给 0 个活跃用户和 1 个不活跃用户,但不活跃用户是 2。并且在几天内不活跃用户带有 -1
    • 你有一封电子邮件,我可以发送一个截图?
    【解决方案2】:

    试试这个...

    SELECT SUM(CASE WHEN type='SUBSCRIBE' THEN 1 ELSE 0 END) as active,
           SUM(CASE WHEN type='UNSUBSCRIBE' THEN 1 ELSE 0 END) as inactive
    WHERE (date>='2013-12-19' and date<='2013-12-24');
    

    【讨论】:

    • 谢谢,但我已经创建了相同的查询;这将返回 SUBSCRIBE 和 UNSUBSCRIBE 的总和,我需要知道每日活跃用户数,
    • @ggmac23 你能解释一下你想要什么吗?你是指当天订阅和退订的总人数吗?
    • 对不起我的英语,我需要看看我有多少人活跃和不活跃的日期,例如:如果用户'5458848'在同一天订阅和取消订阅,这个用户将在接下来的几天内处于非活动状态,直到该用户再次订阅
    • @ggmac23 你的意思是说从 2013-12-19 到 2013-12-24,你想知道活跃用户和非活跃用户的数量?
    • @ggmac23 ,我编辑了代码,如果你想要的话,试试吧:)
    【解决方案3】:

    您需要每个日期的累计订阅数和取消订阅数之间的差值来计算是否有人订阅。例如,最后一个用户7883870 在同一天发生了三个事件后,状态为“订阅”。

    一种方法是使用相关子查询:

    select t.user, t.date,
           (select sum(type = 'SUBSCRIBE') - sum(type = 'UNSUBSCRIBE')
            from table t2
            where t2.user = t.user and
                  t2.date <= t.date
           ) netsubs
    from table t
    group by t.user, t.date;
    

    有了这些信息,我们就可以确定某人在给定日期所处的状态。

    select date, 
           sum(case when netsubs > 0 then 1 else 0 end) as active,
           sum(case when netsubs = 0 then 1 else 0 end) as inactive
    from (select t.user, t.date,
                 (select sum(type = 'SUBSCRIBE') - sum(type = 'UNSUBSCRIBE')
                  from table t2
                  where t2.user = t.user and
                        t2.date <= t.date
                 ) netsubs
          from table t
          group by t.user, t.date
         ) t
    group by t.date;
    

    编辑:

    我误解了这个问题。在任何一天,您都需要在该点之前处于活动状态或已取消订阅的用户数。

    select date, numacive, numusers - numactive as numinactive
    from (select date,
                 (select count(distinct user)
                  from table t2
                  where t2.date <= t.date
                 ) as numusers,
                 (select sum(type = 'SUBSCRIBE') - sum(type = 'UNSUBSCRIBE')
                  from table t2
                  where t2.date <= t.date
                 ) as numactive
          from (select distinct date
                from table t
               ) t
         ) t;
    

    【讨论】:

    • @ggmac23 。 . .第一个查询是否正确累积了订阅和取消订阅的数量?
    • 正如您在查询结果中看到的,2013 年 12 月 23 日当天不活动的用户是 2.. 我怎样才能得到这个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    相关资源
    最近更新 更多