most active time of day based on start and end time

后端 未结 7 1623
悲哀的现实
悲哀的现实 2021-02-08 06:53

I\'m logging statistics of the gamers in my community. For both their online and in-game states I\'m registering when they \"begin\" and when they \"end\". In order to show the

7条回答
  •  花落未央
    2021-02-08 07:29

    If I understood your requirements correctly, if this graph represents user activity:

           Day 
           12/1 12/2 12/3 12/4 ...
    Hour 0  xx    x    x   xx
         1   x   xx        xx
         2 xxx    x    x   xx
         3   x              x
         4        x         x
         5   x              x
         6                  x
       ...
    

    You want to know that 02:00 is the time of the day with the highest average activity (a row with 7 x), and 12/4 was most active day (a column with 10 x). Note that this doesn't imply that 02:00 of 12/4 was the most active hour ever, as you can see in the example. If this is not what you want please clarify with concrete examples of input and desired result.

    We make a couple assumptions:

    • An activity record can start on one date and finish on the next one. For instance: online 2013-12-02 23:35, offline 2013-12-03 00:13.
    • No activity record has a duration longer than 23 hours, or the number of such records is negligible.

    And we need to define what does 'activity' mean. I picked the criteria that were easier to compute in each case. Both can be made more accurate if needed, at the cost of having more complex queries.

    • The most active time of day will be the hour with which more activity records overlap. Note that if a user starts and stops more than once during the hour it will be counted more than once.
    • The most active day will be the one for which there were more unique users that were active at any time of the day.

    For the most active time of day we'll use a small auxiliary table holding the 24 possible hours. It can also be generated and joined on the fly with the techniques described in other answers.

    CREATE TABLE hour ( hour tinyint not null, primary key(hour) );
    INSERT hour (hour)
    VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)
         , (11), (12), (13), (14), (15), (16), (17), (18), (19), (20)
         , (21), (22), (23);
    

    Then the following queries give the required results:

    SELECT hour, count(*) AS activity
      FROM steamonlineactivity, hour
     WHERE ( hour BETWEEN hour(online) AND hour(offline)
          OR hour(online) BETWEEN hour(offline) AND hour
          OR hour(offline) BETWEEN hour AND hour(online) )
     GROUP BY hour
     ORDER BY activity DESC;
    
    SELECT date, count(DISTINCT userID) AS activity
      FROM ( 
           SELECT userID, date(online) AS date
             FROM steamonlineactivity
            UNION
           SELECT userID, date(offline) AS date
             FROM steamonlineactivity
       ) AS x
     GROUP BY date
     ORDER BY activity DESC;
    

提交回复
热议问题