NTILE Function in SQL Server

Creating a Table in SQL Server

The table looks as in the following:

Table-in-sqlserver.jpg

NTILE() Function

Divides the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.

Syntax

NTILE (int_exp) OVER ([<partition_by_clause>]<order_by_clause>)

Example

SELECT *, NTILE(5) OVER (ORDER BY User_Id) AS Ntile_Number

FROM userdetail

OUTPUT

Ntile-function-in-sqlserver.jpg