Group Results in SQL Server using NTILE

The SQL Server NTILE() function divides the result set into a specified number of even sized group (approximate division) and assigns a ranking value to these groups.
The NTILE() function is similar to other ranking functions like the RANK() and DENSE_RANK(), except that NTILE takes one parameter of type int/bigint that specifies the number of groups into which each partition must be divided.
Let us see an example. We will query the Products table of the Northwind database and divide it into 5 groups ordered by the UnitsInStock:
 
SELECT
,[Productname]
,Ntile(5) over (order by unitsinstock desc) as 'Ntile'
FROM [Northwind].[dbo].[Products] 
 
Ebook Download
View all
Learn
View all