Last active
December 24, 2018 09:52
-
-
Save RedaAffane/7cca23107a523e0d137cc8e8e40d1e94 to your computer and use it in GitHub Desktop.
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
SELECT "user_id", | |
"channel", | |
"timestamp" | |
FROM( | |
SELECT *, | |
FIRST_VALUE("touchpoint") over( | |
PARTITION BY "user_id", "user_micro_session" | |
ORDER BY "timestamp") as "channel" | |
FROM( | |
SELECT *, | |
"user_id" || '_' || SUM("new_micro_session") | |
OVER (PARTITION BY "user_id" | |
ORDER BY "timestamp") AS "user_micro_session" | |
FROM ( | |
SELECT | |
*, | |
CASE WHEN | |
EXTRACT(epoch FROM "timestamp") | |
- LAG( | |
EXTRACT(epoch FROM "timestamp") | |
) OVER ( | |
PARTITION BY "user_id" | |
ORDER BY "timestamp" | |
) >= 30 * 60 THEN 1 ELSE 0 END as new_micro_session | |
FROM "log_data" | |
)t1 | |
)t2 | |
)t3 | |
GROUP BY 1,2,3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment