Ntile function
The following is an example of an ntile function
that divides the total sales of each city into four buckets (because
there are six data points, the first two buckets each get an extra
value):
SELECT city, sum(amt), ntile(4) over(ORDER BY sum(amt)) FROM sales_tbl
GROUP BY city;
CITY | SUM | NTILE
------------+-------+-------
Atlanta | 6300 | 1
Boston | 7600 | 1
Seattle | 8700 | 2
Baltimore | 9000 | 2
Los Angeles | 14800 | 3
New York | 16000 | 4
(6 rows)