2017-02-12 25 views
1

表結構包括:SQL不同用戶ID

User_ID , User_Type , Time_Period (integer) 
--------------------------------- 
12345 ,  1  , 201501 
12346 ,  1  , 201501 
12347 ,  2  , 201501 
12345 ,  1  , 201502 
12346 ,  2  , 201502 

隨着時間的推移,

  • 獨特User_IDs可以離開USER_TYPE完全
  • 新User_IDs可以以任何TIME_PERIOD加入
  • User_IDs可以遷移到每個Time_Period中的其他User_Types。

我需要編寫代碼來理解用戶在用戶類型的穩定性超過12個週期,例如,不同User_IDs的70%在USER_TYPE 1停留在週期201501到201512

輸出應該是一個列表由不同User_IDs的計數誰超過12個時間段保持在同一USER_TYPE,和獨特的User_IDs總數在同一時間段

User_Type , Count Distinct Same User_IDs , Count Distinct Total User_IDs 
--------------------------------------------------------------------- 
1   ,    146,023   ,  201,501 
2   ,    46,124   ,  147,234 
3   ,    27,500   ,  87,954 

第二列的USER_TYPE這是我第一次發貼,所以請讓我知道如果你需要更多的細節,並提前致謝

由於每個時間段,編輯 - User_ID可以多次出現在表格中,但每個時段只能出現一次。

+1

編輯你的問題,並提供你想要達到理想的效果。 'Time_Period'的數據類型是什麼?每個月每個用戶是否有一行? –

回答

0

如果每個用戶出現在每個月你想找到存在於所有12個月用戶的比例,並保持「1」,再一個方法是:

select avg(minut = 1 and maxut = 1) 
from (select user_id, min(user_type) as minut, max(user_type) as maxut 
     from t 
     where left(time_period, 4) = '2015' 
     group by user_id 
     having count(*) = 12 
    ) t; 
+0

謝謝你。爲了澄清,儘管每個User_ID可以每個句點有一行作爲表中的記錄,但它們可能不是。如果他們在一段時間內沒有進行交互,那麼User_ID不會在桌面上記錄該時間段。 –

0

這將顯示您用戶的行爲。
每個用戶在其中一行中計數一次。
一行描述了用戶的各個階段 - 其每月的存在時間及其user_type。
如果你會增加你的數據樣本,理解這份報告會更容易。

select  count(*) as users 
      ,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12` 

from  (select  user_id 

         ,min(case when time_period = 201501 then user_type end) as `1` 
         ,min(case when time_period = 201502 then user_type end) as `2` 
         ,min(case when time_period = 201503 then user_type end) as `3` 
         ,min(case when time_period = 201504 then user_type end) as `4` 
         ,min(case when time_period = 201505 then user_type end) as `5` 
         ,min(case when time_period = 201506 then user_type end) as `6` 
         ,min(case when time_period = 201507 then user_type end) as `7` 
         ,min(case when time_period = 201508 then user_type end) as `8` 
         ,min(case when time_period = 201509 then user_type end) as `9` 
         ,min(case when time_period = 201510 then user_type end) as `10` 
         ,min(case when time_period = 201511 then user_type end) as `11` 
         ,min(case when time_period = 201512 then user_type end) as `12` 

      from  mytable 

      group by user_id 
      ) t 

group by `1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12`   

order by users desc  

+-------+---+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ 
| users | 1 | 2  | 3  | 4  | 5  | 6  | 7  | 8  | 9  | 10  | 11  | 12  | 
+-------+---+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ 
| 1  | 1 | 2  | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | 
+-------+---+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ 
| 1  | 2 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | 
+-------+---+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ 
| 1  | 1 | 1  | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | 
+-------+---+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+ 
+0

嗨,感謝您的迴應,這是非常多的,它似乎是一個原始計數與需要了解它是相同的User_ID在每個期間計算任何想法如何添加? –

+0

每個用戶都會被計算一次,並且他所在的那一行描述它在整個月份的移動,無論是類型變化還是dis /出現。如果你在這個報告中有一行users = 12615,這意味着12,615個用戶具有完全相同的行爲,這是描述它的其餘列 –

+0

嗨,它對你有幫助嗎? –