PostgreSQL GROUP BY calculating percentile
<p>i have an table t1 with an 250x250m grid with population and i want to calculate the percentile for each state. i already have the state_id for each cell (st_contains).
now i want to calculate the percentile for each state.</p>

<p>solution, add PARTITION ;)</p>

<pre><code>   SELECT  *,
   ntile(100) OVER(PARTITION BY state_id ORDER BY pop) as percentile
   FROM t1
</code></pre>