+---------------+----------+
| Column Name | Type |
+---------------+----------+
| user_id | int |
| session_start | datetime |
| session_end | datetime |
| session_id | int |
| session_type | enum |
+---------------+----------+
session_id is column of unique values for this table.
session_type is an ENUM (category) type of (Viewer, Streamer).
This table contains user id, session start, session end, session id and session type.
Write a solution to find the the users who have had at least oneconsecutive session of the same type (either ‘Viewer ’ or ‘Streamer ‘) with a maximum gap of 12 hours between sessions.
Return the result table ordered byuser_idinascending order.
Input:
Sessions table:+---------+---------------------+---------------------+------------+--------------+| user_id | session_start | session_end | session_id | session_type |+---------+---------------------+---------------------+------------+--------------+|101|2023-11-0108:00:00|2023-11-0109:00:00|1| Viewer ||101|2023-11-0110:00:00|2023-11-0111:00:00|2| Streamer ||102|2023-11-0113:00:00|2023-11-0114:00:00|3| Viewer ||102|2023-11-0115:00:00|2023-11-0116:00:00|4| Viewer ||101|2023-11-0209:00:00|2023-11-0210:00:00|5| Viewer ||102|2023-11-0212:00:00|2023-11-0213:00:00|6| Streamer ||101|2023-11-0213:00:00|2023-11-0214:00:00|7| Streamer ||102|2023-11-0216:00:00|2023-11-0217:00:00|8| Viewer ||103|2023-11-0108:00:00|2023-11-0109:00:00|9| Viewer ||103|2023-11-0220:00:00|2023-11-0223:00:00|10| Viewer ||103|2023-11-0309:00:00|2023-11-0310:00:00|11| Viewer |+---------+---------------------+---------------------+------------+--------------+Output:
+---------+| user_id |+---------+|102||103|+---------+Explanation:
- User ID 101 will not be included in the final output as they do not have any consecutive sessions of the same session type.- User ID 102 will be included in the final output as they had two viewer sessions with session IDs 3 and 4, respectively, and the time gap between them was less than 12 hours.- User ID 103 participated in two viewer sessions with a gap of less than 12 hours between them, identified by session IDs 10 and 11. Therefore, user 103 will be included in the final output.Output table is ordered by user_id in increasing order.
We need to find users who have at least one pair of consecutive sessions of the same type with a gap of at most 12 hours. We can use window functions (LAG/LEAD) or a self-join to compare each session with the next one for the same user and type.
SELECTDISTINCT user_id
FROM (
SELECT user_id, session_type,
session_start,
LAG(session_end) OVER (PARTITION BY user_id, session_type ORDERBY session_start) AS prev_end
FROM Sessions
) t
WHERE prev_end ISNOTNULLAND TIMESTAMPDIFF(HOUR, prev_end, session_start) <=12ORDERBY user_id;
1
2
3
4
5
6
7
8
9
10
SELECTDISTINCT user_id
FROM (
SELECT user_id, session_type,
session_start,
LAG(session_end) OVER (PARTITION BY user_id, session_type ORDERBY session_start) AS prev_end
FROM Sessions
) t
WHERE prev_end ISNOTNULLAND session_start - prev_end <= INTERVAL '12 hours'ORDERBY user_id;