PostgreSQL – NTILE Function

The NTILE() function is used to distribute the rows of a result set into a specified number of roughly equal-sized “tiles” or groups. Each row is assigned a tile number based on the distribution. This function is often used for data segmentation and percentile calculations, especially when you want to divide your data into quantiles or quartiles.

The syntax of the NTILE() function is as follows:

NTILE(num_tiles) OVER (PARTITION BY partition_expression ORDER BY sort_expression)
  • num_tiles: The number of tiles or groups you want to divide the result set into.
  • PARTITION BY: An optional clause that divides the result set into partitions or groups. The function operates separately within each partition. If omitted, the function operates on the entire result set.
  • ORDER BY: Specifies the column(s) by which you want to order the result set for dividing the rows into tiles.

Here’s a simple example of how to use the NTILE() function in PostgreSQL:

Suppose you have a table named “students” with columns “student_name” and “score”, and you want to divide students into three roughly equal-sized groups based on their scores:

SELECT student_name, score, NTILE(3) OVER (ORDER BY score) AS score_tile FROM students;

In this example, the NTILE() function divides the students into three tiles based on their scores in ascending order. The result might look like this:

student_name | score | score_tile

-------------+-------+------------

Alice | 80 | 1

Bob | 85 | 1

Charlie | 90 | 2

David | 92 | 2

Emily | 95 | 3

Frank | 98 | 3

In this output, the “score_tile” column contains the tile number assigned to each student based on their scores. The rows are roughly divided into three equal-sized groups, and the students within each group have similar scores.

The NTILE() function is useful when you want to evenly distribute your data into a specific number of groups, especially for calculating percentiles or quartiles in your dataset.

Similar Posts