NTILE

Divides the rows for each window partition into a number of buckets. The number of buckets ranges from 1 to n. If there is a remainder, the extra rows are distributed one per bucket, starting with the first bucket. If there are fewer rows than n, the number of existing rows is expressed.

ParameterTypeDescription
buckets
integer
the number of groupings to create
Returns: any

Example

query:

SELECT account, order_value, NTILE(4) OVER ( PARTITION BY account ORDER BY order_value desc)
  FROM jun_2017_orders

result:

account order_value ntile
Bioplex 6229 1
Bioplex 2993 1
Bioplex 1109 2
Bioplex 53 3
Bioplex 52 4
Kan-code 5393 1
Kan-code 5339 1
Kan-code 4712 1
Kan-code 4612 1
Kan-code 4369 1
Kan-code 3893 2
Kan-code 3558 2
Kan-code 3290 2
Kan-code 2928 2
Kan-code 1120 3
Kan-code 899 3
Kan-code 581 3