Created
January 8, 2015 16:35
-
-
Save randyzwitch/435172d840e4c1243100 to your computer and use it in GitHub Desktop.
Sessionizing log file data using SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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