2017-02-12 6 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_IDsがどのTIME_PERIODで添加することができるUSER_TYPEを残すことができます
  • User_IDは、各Time_Periodで他のUser_Typesに移行できます。私は例えば、個別のUser_IDsの70%が期間にわたってUSER_TYPE 1にとどまっ201501

    201512への出力は、リストでなければなりません、12の期間にわたってユーザータイプのユーザーの安定性を理解するためのコードを記述する必要があり

同じ期間で12時間の期間にわたって同じUSER_TYPEに残った明確なUser_IDsの数、および個別のUser_IDsの総数の2列目のUSER_TYPE

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 

によってこれには、私の初めての投稿です詳細やお返事が必要な場合は事前にお知らせください

EDIT - User_IDは、期間ごとに複数回表示されますが、期間ごとに1回のみ表示されます。

+1

編集あなたの質問をし、あなたが達成したい目的の結果を提供します。 'Time_Period'のデータ型は?毎月ユーザーごとに1つの行がありますか? –

答えて

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はテーブルのレコードとして期間ごとに1つの行を持つことができますが、そうでないかもしれません。期間内に相互作用しなかった場合、その期間のUser_IDはテーブルに記録されません。 –

0

これはあなたが表示されますユーザーの行動
各ユーザーは、いずれかの行で1回カウントされます。
行には、ユーザーのフェーズが記述されています。そのフェーズは、毎月の存在とその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

各ユーザーは1回カウントされ、カウントされた行は、タイプ変更または表示/非表示のいずれかであるかどうかの月間の動きを示します。このレポートにユーザー= 12615の行がある場合、残りの列には12,615人のユーザーが同じ動作をしていることを意味します –

+0

こんにちは、それはあなたにとって有益でしたか? –

関連する問題