Saturday, 6 December 2014

SQL Server ranking functions

In SQL Server there is 4 ranking functions:
ROW_NUMBER
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
RANK
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
DENSE_RANK
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.
NTILE
Distributes 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.
below is the syntax:
ROW_NUMBER ( )     OVER ( [ <partition_by_clause> ] <order_by_clause> )
RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > )
DENSE_RANK ( )    OVER ( [ < partition_by_clause > ] < order_by_clause > ) 
NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > )



No comments:

Post a Comment