Skip to content

Instantly share code, notes, and snippets.

@mhkeller
Created September 20, 2018 15:47
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 mhkeller/2bcd8f476b80f8bc7430e55b12a59322 to your computer and use it in GitHub Desktop.
Save mhkeller/2bcd8f476b80f8bc7430e55b12a59322 to your computer and use it in GitHub Desktop.
Calculat quantile amounts based on aggregate counts. Adapted from https://gist.github.com/hrwgc/5171860
SELECT
ntile,
avg(ct) AS avgAmount,
max(ct) AS maxAmount,
min(ct) AS minAmount,
median(ct) as median
FROM (SELECT ct, ntile(5) OVER (ORDER BY ct) AS ntile FROM (select count(col_name) as ct from table GROUP BY col_Name) t) x
GROUP BY ntile
ORDER BY ntile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment