Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Created January 8, 2015 16:12
Show Gist options
  • Save randyzwitch/caed86f89a3c69644f3a to your computer and use it in GitHub Desktop.
Save randyzwitch/caed86f89a3c69644f3a to your computer and use it in GitHub Desktop.
Postgres Sessionize Logfile data
select
uid,
sum(new_event_boundary) OVER (PARTITION BY uid ORDER BY event_timestamp) as session_id,
event_timestamp,
minutes_since_last_interval,
new_event_boundary
from
--Query 1: Define boundary events
(select
uid,
event_timestamp,
(extract(epoch from event_timestamp) - lag(extract(epoch from event_timestamp)) OVER (PARTITION BY uid ORDER BY event_timestamp))/60 as minutes_since_last_interval,
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