Event analytics: choose the output row
- Sessions: one row per
(user_id, session_index). - Snapshots: one eligible event per device.
- Prerequisite: filters, grouping, timestamps, CTEs.
Predict the row count before choosing a window function.
1 / 7
(user_id, session_index).Predict the row count before choosing a window function.
One user's times: 09:00, 09:30, 10:00, 10:31, 10:31.
Which events start a session? Does the first session exceed 30 minutes?
| time | new? | index |
|---|---|---|
| 09:00 | 1 | 1 |
| 09:30 | 0 | 1 |
| 10:00 | 0 | 1 |
| 10:31 | 1 | 2 |
| 10:31 | 0 | 2 |
The rule limits adjacent gaps, not total session duration.
LAG: partition by user, order by (event_ts, event_id).ROWS frame.Carry the tie breaker through each intermediate CTE.
At the midnight cutoff, a device has:
Which row wins? What goes wrong if ranking happens before filtering?
A final output sort cannot fix an ambiguous window order.
Build Event Sessions, then Latest Device State, then the checkpoint.
Before submitting, test: an exact 30-minute gap, tied times, another user, a cutoff-time event, and a device with only future events.