The user_actions table logs one row per action a user takes. Group each user's actions into sessions: a new session starts when the gap to the same user's previous action is strictly greater than 30 minutes, and each user's first action starts session 1.
For each action, return the action itself, its session number, the time of the user's previous action and the gap in minutes since that action.
Input Table(s)
Table user_actions (primary key action_id):
| Column | Type | Meaning |
|---|
| action_id | INT | Id of the action. Primary key. |
| user_id | INT | User who took the action. Not null. |
| action_time | TIMESTAMP | When the action happened. Not null. |
| action_type | VARCHAR(50) | Kind of action, for example login or view. Not null. |
A user can have many rows.
Output Contract
One row per row of user_actions, with these columns in this order:
| Column | Meaning |
|---|
| user_id | From the input row |
| action_id | From the input row |
| action_time | From the input row |
| action_type | From the input row |
| session_id | Session number within the user, starting at 1 and going up by 1 at each new session |
| prev_action_time | action_time of the same user's previous action, NULL for the user's first action |
| gap_minutes | Minutes from prev_action_time to action_time, rounded to 2 decimals, NULL for the user's first action |
A user's previous action is the row just before it when that user's actions are ordered by action_time, then action_id.
Order the result by user_id, action_time, action_id, all ascending. action_id is unique, so the order has no ties.
Example
user_actions:
| action_id | user_id | action_time | action_type |
|---|
| 1 | 10 | 2024-03-01 09:00:00 | login |
| 2 | 10 | 2024-03-01 09:12:30 | view |
| 3 | 10 | 2024-03-01 09:42:30 | click |
| 4 | 10 | 2024-03-01 10:12:31 | view |
| 5 | 20 | 2024-03-01 09:05:00 | view |
| 6 | 20 | 2024-03-01 09:05:00 | click |
| 7 | 20 | 2024-03-01 11:00:00 | logout |
Expected result:
| user_id | action_id | action_time | action_type | session_id | prev_action_time | gap_minutes |
|---|
| 10 | 1 | 2024-03-01 09:00:00 | login | 1 | NULL | NULL |
| 10 | 2 | 2024-03-01 09:12:30 | view | 1 | 2024-03-01 09:00:00 | 12.50 |
| 10 | 3 | 2024-03-01 09:42:30 | click | 1 | 2024-03-01 09:12:30 | 30.00 |
| 10 | 4 | 2024-03-01 10:12:31 | view | 2 | 2024-03-01 09:42:30 | 30.02 |
| 20 | 5 | 2024-03-01 09:05:00 | view | 1 | NULL | NULL |
| 20 | 6 | 2024-03-01 09:05:00 | click | 1 | 2024-03-01 09:05:00 | 0.00 |
| 20 | 7 | 2024-03-01 11:00:00 | logout | 2 | 2024-03-01 09:05:00 | 115.00 |
Action 3 comes exactly 30 minutes after action 2, so it stays in session 1. Action 4 comes 30 minutes and 1 second after action 3, so it starts session 2. Actions 5 and 6 share a timestamp: action_id puts 5 first, and action 6 gets a gap of 0.00 in the same session.
Constraints and Clarifications
-
user_actions
has 1 to 10,000 rows.
-
A gap of exactly 30 minutes stays in the same session. Only a gap strictly greater than 30 minutes starts a new one, judged on the exact gap before rounding.
-
A user can have several actions with the same
action_time
. Order them by
action_id
; the later one has a gap of 0.00 minutes.
-
No column of
user_actions
is NULL. The only NULLs in the output are
prev_action_time
and
gap_minutes
on each user's first action.
-
Session numbers restart at 1 for every user.