-
-
Save kiyoto/f904c2368a3ac3ebfbc92a5230b588ad 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
WITH by_week | |
AS (SELECT | |
user_id, | |
TD_DATE_TRUNC('week', login_time) AS login_week | |
FROM logins | |
GROUP BY 1, 2), | |
with_first_week | |
AS (SELECT | |
user_id, | |
login_week, | |
FIRST_VALUE(login_week) OVER (PARTITION BY user_id ORDER BY login_week) AS first_week | |
FROM by_week) | |
SELECT | |
user_id, | |
login_week, | |
first_week, | |
(login_week - first_week) / (24 * 60 * 60 * 7) AS week_number | |
FROM with_first_week |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment