Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created January 8, 2015 16:35
Show Gist options
  • Save randyzwitch/435172d840e4c1243100 to your computer and use it in GitHub Desktop.
Save randyzwitch/435172d840e4c1243100 to your computer and use it in GitHub Desktop.
Sessionizing log file data using SQL
--Query 3: Outer query uses window function with sum to do cumulative sum as the id, concatentate to uid
select
uid,
uid || '-' || cast(sum(new_event_boundary) OVER (PARTITION BY uid ORDER BY event_timestamp) as varchar) as session_id,
event_timestamp
from
--Query 1: Define boundary events
(select
uid,
event_timestamp,
case when extract(epoch from event_timestamp) - lag(extract(epoch from event_timestamp)) OVER (PARTITION BY uid ORDER BY event_timestamp) > 30 * 60 then 1 ELSE 0 END as new_event_boundary
from single_col_timestamp
) a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment