1

我试图弄清楚如何编写我的 SQL 查询来获取用户的日常使用和保留。考虑在每轮比赛中使用以下行表 round_statistics 我有该轮的日期,现在我想: 1. 知道有多少用户连续两天玩意味着在周日和周一、周一和周二玩,但周日和星期二不算连续两天。2. 用户留存 1-7

留存率 7 是:在过去 7 天有机会玩游戏(意味着他们至少注册了 7 天)并在 7 天后有一些活动(记录)的用户百分比。

保留 6-1 仅在 6-1 天内相同。

请帮我找出我的游戏保留 :) 你会得到一个免费的硬币来玩它....谢谢。

表结构为:user_id,round_time

例如,如果我今天玩了 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'

结果结构为:

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   
4

1 回答 1

2

我的 sql 没有分析功能,也没有 CTE 和数据透视表功能,因此不能直接执行您所需的查询(也没有人回答您的问题)。

对于此数据:

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');

对于给定日期,这是在枢轴保留之前的一种方法( '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

您的预煮数据(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 |

这是(对于相同的数据):

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

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 |
于 2013-08-11T15:36:02.080 回答