Last active
February 6, 2022 10:28
-
-
Save Mr--John-Doe/da1943ec7acdd296c860c2be8b1f18d0 to your computer and use it in GitHub Desktop.
This file contains 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
.... | |
SET FROM_DATE = $RUN_DATE-7; | |
SET TO_DATE = $RUN_DATE; | |
/* We don't need much from before $FROM_DATE, just the max(login_date) */ | |
CREATE TEMP TABLE temp_previous_login__pre_window AS | |
SELECT username AS username | |
, MAX(login_date) AS pre_window_login_date | |
FROM sales_site.login | |
WHERE login_date < $FROM_DATE | |
GROUP BY 1; | |
CREATE TEMP TABLE temp_previous_login AS | |
WITH base as ( | |
SELECT * | |
, LAG(login_date) OVER (PARTITION BY username ORDER BY date) AS previous_login_date__in_window | |
FROM sales_site.login | |
WHERE login_date between $FROM_DATE and $TO_DATE | |
) | |
SELECT cur.* | |
, coalesce(previous_login_date__in_window, -- null means it happened before $START_DATE, or never at all | |
pre_window_login_date) as previous_login_date | |
FROM temp_previous_login curr | |
LEFT JOIN sales_site.login prev | |
on curr.username = prev.username | |
; | |
.... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment