2015-05-27 46 views
3

表結構是:USER_ID,日期(我使用時間戳工作)MySQL的每日計數新用戶VS返回的用戶(隊列分析)

例如

user id | Date (TS) 
A  | '2014-08-10 14:02:53' 
A  | '2014-08-12 14:03:25' 
A  | '2014-08-13 14:04:47' 
B  | '2014-08-13 04:04:47' 
... 

併爲下一個本週我有

user id | Date (TS) 
A  | '2014-08-17 09:02:53'  
B  | '2014-08-17 10:04:47' 
B  | '2014-08-18 10:04:47' 
A  | '2014-08-19 10:04:22' 
C  | '2014-08-19 11:04:47' 
... 

,併爲今天,我有

user id | Date (TS) 
A  | '2015-05-27 09:02:53'  
B  | '2015-05-27 10:04:47' 
C  | '2015-05-27 10:04:22' 
D  | '2015-05-27 17:04:47' 

我需要知道如何執行單個查詢來查找從其活動一開始就是「返回」用戶的用戶數量。

預期結果:

date  | New user | returned User 
2014-08-10 | 1  | 0 
2014-08-11 | 0  | 0 
2014-08-12 | 0  | 1 (A was active on 08/11) 
2014-08-13 | 1  | 1 (A was active on 08/12 & 08/11) 
... 
2014-08-17 | 0  | 2 (A & B were already active) 
2014-08-18 | 0  | 1 
2014-08-19 | 1  | 1 
... 
2015-05-27 | 1  | 3 (D is a new user) 

#2一些長期搜索後,我發現這裏https://meta.stackoverflow.com/users/107744/spencer7593提供的一些材料:Weekly Active Users for each day from log但我並沒有繼承他的查詢更改輸出我的預期結果。

感謝您的幫助

回答

3

假設你有一個日期表的地方(而使用T-SQL語法,因爲我知道它更好的... ...),關鍵是要分別計算每個用戶的MINDATE,計算出總在這一天的用戶數,然後就宣告迴歸用戶是誰沒有新用戶:

SELECT DateTable.Date, NewUsers, NumUsers - NewUsers AS ReturningUsers 
FROM 
DateTable 
    LEFT JOIN 
     (
     SELECT MinDate, COUNT(user_id) AS NewUsers 
     FROM (
       SELECT user_id, min(CAST(date AS Date)) as MinDate 
       FROM Table 
       GROUP BY user_id 
      ) A 
     GROUP BY MinDate 
     ) B ON DateTable.Date = B.MinDate 
    LEFT JOIN 
     (
     SELECT CAST(date AS Date) AS Date, COUNT(DISTINCT user_id) AS NumUsers 
     FROM Table 
     GROUP CAST(date AS Date) 
     ) C ON DateTable.Date = C.Date 
1

感謝斯蒂芬,我對他的查詢,它工作得很好,即使它是一個做了簡短的修復在大型數據庫上消耗大量時間:

SELECT 
    DATE(Stats.Created), 
    NewUsers, 
    NumUsers - NewUsers AS ReturningUsers 
FROM 
    Stats 
LEFT JOIN 
    (
     SELECT 
      MinDate, 
      COUNT(user_id) AS NewUsers 
     FROM (
      SELECT 
       user_id, 
       MIN(DATE(Created)) as MinDate 
      FROM Stats 
      GROUP BY user_id 
     ) A 
     GROUP BY MinDate 
    ) B 
ON DATE(Stats.Created) = B.MinDate 
LEFT JOIN 
    (
     SELECT 
      DATE(Created) AS Date, 
      COUNT(DISTINCT user_id) AS NumUsers 
     FROM Stats 
     GROUP BY DATE(Created) 
    ) C 
ON DATE(Stats.Created) = C.Date 
GROUP BY DATE(Stats.Created)