-
-
Save izzymiller/b894c6a3fd6146ff7b8d87e792bc2a94 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 chat,sender,text, | |
| message_date, | |
| SUM(is_new_session) OVER (ORDER BY chat, message_date) AS global_session_id, | |
| SUM(is_new_session) OVER (PARTITION BY chat ORDER BY message_date) AS chat_session_id | |
| FROM ( | |
| SELECT *, | |
| CASE WHEN EXTRACT('EPOCH' FROM message_date) - EXTRACT('EPOCH' FROM last_event) >= (60 * 120) | |
| OR last_event IS NULL | |
| THEN 1 ELSE 0 END AS is_new_session | |
| FROM ( | |
| SELECT chat,sender,text, | |
| message_date, | |
| LAG(message_date,1) OVER (PARTITION BY chat ORDER BY message_date) AS last_event | |
| FROM messages | |
| ) last | |
| ) final |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment