Skip to content

Instantly share code, notes, and snippets.

@kiyoto
Last active August 27, 2016 23:40
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/521160673fb876471232da986372e471 to your computer and use it in GitHub Desktop.
Save kiyoto/521160673fb876471232da986372e471 to your computer and use it in GitHub Desktop.
Percentile Usage User Segmentation
WITH login_frequency AS (
SELECT user_id,
COUNT(login) AS num_logins
FROM logins
GROUP BY user_id)
SELECT user_id,
CASE NTILE(4) OVER (ORDER BY num_logins)
WHEN 1 THEN ‘bottom_25’
WHEN 4 THEN ‘top_25’
ELSE ‘middle_50’
END AS usage_segment
FROM login_frequency
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment