The NTILE Function

The NTILE function assigns a ranking value by separating the result rows of a query into a specified number of approximately even-sized groups. Each group of rows is assigned the same ranking number, starting with 1 for the first group, 2 for the second, and so on. You specify the number of groups you want the result set divided into as the argument to the NTILE function. The number of rows in a group is determined by dividing the total number of rows in the result set by the number of groups. If there’s a remainder, n, the first n groups have an additional row assigned to them. Listing 43.48 provides an example of using the NTILE function, so you can compare it to the ROW_NUMBER function.

Listing 43.48 Using the NTILE Function ...

Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.