Skip to content

Instantly share code, notes, and snippets.

@kiyoto
Created July 19, 2016 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiyoto/f904c2368a3ac3ebfbc92a5230b588ad to your computer and use it in GitHub Desktop.
Save kiyoto/f904c2368a3ac3ebfbc92a5230b588ad to your computer and use it in GitHub Desktop.
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