MySQL grouping results by time periods

I have a table that contains timestamps of session events. I want to ask how many sessions I have according to the timestamps when 2 sessions are separated by a difference of at least 10 minutes. Can I read sessions using a MySql query?

thank

+1
source share
1 answer

With a little information about your tables, this is nothing more than a basic idea for you, but you can do something like this: -

SELECT COUNT(*)
FROM
(
    SELECT a.TimeStamp AS ThisTimeStamp, MIN(b.TimeStamp) AS NextTimeStamp
    FROM SomeTable a
    INNER JOIN SomeTable b
    ON a.TimeStamp < b.TimeStamp
    GROUP BY a.TimeStamp
) Sub1
WHERE Sub1.ThisTimeStamp < (Sub1.NextTimeStamp - 600)

, , MIN, . , 600 ( , unix).

. , 10- + , :

SELECT COUNT(*)
FROM
(
    SELECT a.user_id, a.TimeStamp AS ThisTimeStamp, MIN(b.TimeStamp) AS NextTimeStamp
    FROM SomeTable a
    INNER JOIN SomeTable b
    ON a.TimeStamp < b.TimeStamp
    AND a.user_id = b.user_id
    GROUP BY a.user_id, a.TimeStamp
) Sub1
WHERE Sub1.ThisTimeStamp < (Sub1.NextTimeStamp - 600)
+2

Source: https://habr.com/ru/post/1679615/


All Articles