用户类型的SQL不同用户ID随时间的变化

时间:2017-02-12 15:29:17

标签: mysql sql

表结构包括:

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

随着时间的推移,

  • 唯一的User_ID可以完全保留User_Type
  • 可以在任何Time_Period
  • 中添加新的User_ID
  • User_ID可以迁移到每个Time_Period中的其他User_Types。

我需要编写代码来了解用户类型超过12个周期的用户的稳定性,例如,70%的不同User_ID在201501到201512期间保留在User_Type 1中

输出应该是User_Type的列表,其中列出了在12个时间段内保持相同User_Type的不同User_ID的计数,以及同一时间段内不同User_ID总数的第二列

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_ID可以多次出现在表格中,但每个时段只能出现一次。

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

这将显示用户的行为 每个用户在其中一行中计数一次 一行描述了用户的阶段 - 每个月都存在的用户类型和用户类型 如果您增加数据样本,则更容易理解此报告。

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) |
+-------+---+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+