简体   繁体   English

MySQL用户保留和日常

[英]MySQL user retention and day to day

I'm trying to figure out how to write my SQL query to get users day to day and retention. 我试图弄清楚如何编写我的SQL查询来获得用户的日常和保留。 consider having the following row table round_statistics on each play round i have date of the round, now i would like to: 1. know how many users play two days in a row meaning played on Sunday and Monday, Monday and Tuesday, but Sunday and Tuesday doesn't count as two days in a row. 考虑在每个比赛回合中都有以下行表round_statistics我有该回合的日期,现在我想:1.知道连续两天有多少用户玩过,意思是在星期日和星期一,星期一和星期二玩,但是星期日和星期二不算作连续两天。 2. users retention 1-7 2.用户保留1-7

retention 7 is : % of users that have the chance to play the last 7 days (meaning they are registered at least 7 days) and had some activity (record) after 7 days. 保留7是:最近7天有机会玩(意味着他们至少注册7天)并在7天后有一些活动(记录)的用户百分比。

retention 6-1 are the same only for 6-1 days. 保留时间6-1仅在6-1天相同。

Please help me to find out my game retention :) you will get a free coins to play it.... Thanks. 请帮助我找出我的游戏保持力:)您将获得免费的硬币来玩..谢谢。

The table structure is: user_id,round_time 该表的结构为:user_id,round_time

for example if i played 3 times today: 例如,如果我今天玩了3次:

user id | round_time
1000,   | '2013-08-10 14:02:53' 
1000,   | '2013-08-10 14:03:25' 
1000,   | '2013-08-10 14:04:47'

the result structure is: 结果结构为:

date        |  2013-08-10 |   2013-07-10 
day to day  |  10         |   100         
retention 7 |  15         |   125         
retention 6 |  20         |   210         
retention 5 |  30         |   320         
retention 4 |  40         |   430         
retention 3 |  50         |   540         
retention 2 |  60         |   650         
retention 1 |  120        |   1620   

My sql don't has analytic functions , neither CTE and pivot table features, for this reasons it is not direct to do your required query (and nobody answer your question). 我的sql没有解析功能 ,也没有CTE和数据透视表功能,因此,直接进行所需的查询不是很直接(而且没人回答您的问题)。

For this data: 对于此数据:

create table t ( uid int, rt date);
insert into t values 
(99,    '2013-08-7 14:02:53' ),     <- gap
(99,    '2013-08-9 14:02:53' ),     <-
(99,    '2013-08-10 14:03:25' ),
(1000,    '2013-08-7 14:02:53' ),
(1000,    '2013-08-8 14:03:25' ),
(1000,    '2013-08-9 14:03:25' ),
(1000,   '2013-08-10 14:04:47');

This is an approach before pivot retentions, for a given date ( '2013-08-10 00:00:00' , '%Y-%m-%d') : 这是在给定日期( '2013-08-10 00:00:00' , '%Y-%m-%d')进行数据透视保留之前的一种方法:

select count( distinct uid ) as n, d, dt from
(
  select uid,
         '2013-08-10 00:00:00' as d,
         G.dt      
  from 
    t
  inner join
    ( select 7 as dt union all 
      select 6 union all select 5 union all
      select 4 union all select 3 union all
      select 2 union all select 1 union all select 0) G
  on DATE_FORMAT( t.rt, '%Y-%m-%d') between
        DATE_FORMAT( date_add( '2013-08-10 00:00:00', Interval -1 * G.dt DAY) , 
                    '%Y-%m-%d')
     and
        DATE_FORMAT(  '2013-08-10 00:00:00' , '%Y-%m-%d')
  where DATE_FORMAT(rt , '%Y-%m-%d') <= DATE_FORMAT(  '2013-08-10 00:00:00' , 
                                                      '%Y-%m-%d')
  group by uid, G.dt
  having  count( distinct DATE_FORMAT( T.rt, '%Y-%m-%d') )  = G.dt + 1
) TT
group by dt

Your pre-cooked data ( DT = 0 means today visits, DT = 1 means 2 consecutive days, ...): 您的预煮数据(DT = 0表示今天访问,DT = 1表示连续2天,...):

| N |                   D | DT |
--------------------------------
| 2 | 2013-08-10 00:00:00 |  0 |
| 2 | 2013-08-10 00:00:00 |  1 |
| 1 | 2013-08-10 00:00:00 |  2 |
| 1 | 2013-08-10 00:00:00 |  3 |

Here it is ( for same data ): 这是(对于相同数据):

select count( distinct uid ) as n, d, dt from
(
  select uid,
         z.zt as d,
         G.dt      
  from 
    t
  cross join
     ( select distinct DATE_FORMAT( t.rt, '%Y-%m-%d') as zt from t) z
  inner join
    ( select 7 as dt union all 
      select 6 union all select 5 union all
      select 4 union all select 3 union all
      select 2 union all select 1 union all select 0) G
  on DATE_FORMAT( t.rt, '%Y-%m-%d') between
        DATE_FORMAT( date_add( z.zt, Interval -1 * G.dt DAY) , 
                    '%Y-%m-%d')
     and
        z.zt
  where z.zt <= z.zt
  group by uid, G.dt, z.zt
  having  count( distinct DATE_FORMAT( T.rt, '%Y-%m-%d') )  = G.dt + 1
) TT
group by d,dt
order by d,dt

Results at sqlfiddle: http://sqlfiddle.com/#!2/c26ec/10/0 sqlfiddle的结果: http ://sqlfiddle.com/#!2/c26ec/10/0

| N |          D | DT | GROUP_CONCAT( UID) |
--------------------------------------------
| 2 | 2013-08-07 |  0 |            1000,99 |
| 1 | 2013-08-08 |  0 |               1000 |
| 1 | 2013-08-08 |  1 |               1000 |
| 2 | 2013-08-09 |  0 |            1000,99 |
| 1 | 2013-08-09 |  1 |               1000 |
| 1 | 2013-08-09 |  2 |               1000 |
| 2 | 2013-08-10 |  0 |            1000,99 |
| 2 | 2013-08-10 |  1 |            99,1000 |
| 1 | 2013-08-10 |  2 |               1000 |
| 1 | 2013-08-10 |  3 |               1000 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM