Turn a Session Rule into Tested SQLite SQL with ChatGPT
“Group events into sessions” leaves several important decisions unstated. This tutorial gives ChatGPT a precise inactivity rule, then verifies the returned SQLite query against tied timestamps, exact 30-minute gaps and a late event that joins two earlier sessions. All device events are fictional.
Write the rule and counterexamples first
Within each device, order events by timestamp and event ID. The first event starts a session. A later event starts another session only when its gap from the previous event is greater than 30 minutes. Exactly 30 minutes stays in the current session; 31 minutes does not. Two events with the same timestamp remain two source records.
Write a read-only SQLite query over events(device_id, event_id, event_ts).
All timestamps use one consistent UTC text format.
Partition by device and order by event_ts, event_id.
Start a new session only for the first event or a gap > 1800 seconds.
Return device, session number, start, end and event count.
Count tied events and explain what late arrivals do to session numbers.Give ChatGPT the schema and sample records, then save and review the returned query.sql. Do not substitute half-hour clock buckets: 09:25 and 09:35 are only ten minutes apart even though they sit in different such buckets.
Inspect the three stages of the query
The recorded query first converts timestamps to integer epoch seconds and uses LAG to obtain the preceding event’s time. It then marks session starts and calculates a cumulative count. SQLite documents the ordering and frame behavior in its window-function reference; its date-function reference describes the epoch conversion.
-- Previous-event step, within a CTE that also supplies epoch_seconds:
LAG(epoch_seconds) OVER (
PARTITION BY device_id
ORDER BY event_ts, event_id
) AS previous_epoch_seconds
-- Mark the first event or an inactivity gap:
CASE WHEN previous_epoch_seconds IS NULL
OR epoch_seconds - previous_epoch_seconds > 1800
THEN 1 ELSE 0 END AS starts_session
-- Cumulative numbering in a later CTE:
SUM(starts_session) OVER (
PARTITION BY device_id
ORDER BY event_ts, event_id
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS session_noThese excerpts are successive stages, not one standalone SELECT. Keep the complete reviewed query’s CTEs in their original order, then group by device and session number to calculate MIN(event_ts), MAX(event_ts) and COUNT(*). The event ID supplies deterministic order for timestamp ties.
Verify the starting output
Load the practice schema and its original six events into a separate SQLite database. Execute the unchanged query and compare the complete result, including each device’s start, end and count. The recorded starting output contains four sessions accounting for all six events. Device A’s two tied events at 11:01 both belong to its second session.
Check conservation as well as grouping: the sum of session event counts must equal the number of source events. A query that accidentally drops one of the ties can produce plausible session boundaries while giving the wrong total.
Add the fresh events without editing the query
A’s 11:31 event is exactly 30 minutes after 11:01, so it extends that session. Its 12:02 event follows a 31-minute gap and starts another. For B, a late 09:30 event bridges the original 09:00 and 10:00 events: recomputing the full ordered history now gives one continuous session for that device.
The independent check verified both exact result sets, all nine final events, tied records and the unchanged read-only query. The session numbers are derived positions, not permanent identifiers: late arrivals can change them. An incremental production pipeline needs an explicit policy for recomputation and downstream identifiers.
This lesson assumes valid timestamps in a consistent, sortable UTC representation. Normalize or reject mixed formats before reusing text ordering. For a separate example of verifying numeric aggregates, see exact SQLite totals with integer cents. The UTC interval guide illustrates why equality at a time boundary should be specified explicitly. More lessons are in Tutorials.
James Kennedy is a writer and product researcher at Drives Hero with a background in IT administration and consulting. He has hands-on experience with storage, networking, and system performance, and regularly improves and optimizes his home networking setup.


