Created
November 13, 2012 00:42
-
-
Save andrewxhill/4063106 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 clusters as ( | |
SELECT | |
cartodb_id, | |
st_snaptogrid(the_geom_webmercator, CDB_XYZ_Resolution({z})) as center | |
FROM table_2007_2009_bicy | |
), | |
points as ( | |
SELECT | |
count(cartodb_id) as npoints, | |
count(cartodb_id)/power( CDB_XYZ_Resolution({z}), 2 ) as density | |
FROM | |
clusters | |
group by | |
center | |
), | |
stats as ( | |
SELECT | |
npoints, | |
density, | |
ntile(5) over (order by density) as quartile | |
FROM points | |
) | |
SELECT | |
quartile, | |
max(npoints) as maxAmount, | |
max(density) as maxDensity | |
FROM stats | |
GROUP BY quartile ORDER BY quartile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment