Exercise · SQL & Querying
Predict, then run it
Predict the output
obs(volunteer, ts) has tide-pool observations for volunteers A and B. Number each volunteer's survey visits with a 2-hour timeout, partitioned per volunteer so sessions don't bleed:
WITH flagged AS (SELECT volunteer, ts, CASE WHEN ts - LAG(ts) OVER (PARTITION BY volunteer ORDER BY ts) > INTERVAL '2 hours' OR LAG(ts) OVER (PARTITION BY volunteer ORDER BY ts) IS NULL THEN 1 ELSE 0 END AS new_session FROM obs) SELECT volunteer, strftime(ts, '%H:%M') AS mark, SUM(new_session) OVER (PARTITION BY volunteer ORDER BY ts) AS session_id FROM flagged ORDER BY volunteer, ts;
Predict the exact output.