Back to problems

Detect sessions and gaps using SQL LEAD

SQL · Apple · Medium

You are given a table events with the columns user_id (integer), ts (timestamp), and action (text). Each row records one user action. Write one ANSI-SQL query that groups each user's events into sessions. For a given user, a new session starts only when the time difference from that user's previous event is strictly greater than 30 minutes. If the previous event was no more than 30 minutes earlier (<= 30 minutes, including exactly 30 minutes), the current event continues the…

Checking your access…