Monday, October 22, 2012

Rank Function and its types


Rank Function and its types 

Ranking functions return a ranking value for each row in a partition.
 All the ranking functions are non-deterministic. The different Ranking functions are as follows:

ROW_NUMBER () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of each row within the partition of a result set.

DENSE_RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of rows within the partition of a result set, without any gaps in the ranking.


Ntile()
Distributes the rows in an ordered partition into a specified number of groups.
NTILE (integer_expression)    OVER ( [ <partition_by_clause> ] < order_by_clause > )


The Queries for all rank functions is :
SELECT      NAMEOFTHEBANK,
            Branches,
            RANK() OVER(ORDER BY Branches DESC) AS RANKID,
            ROW_NUMBER() OVER(ORDER BY Branches DESC) AS ROWNO,
            DENSE_RANK() OVER(ORDER BY Branches DESC) AS DENSERANK,
            NTILE(3) OVER(ORDER BY Branches DESC) AS NT
FROM tbl_Banks
GROUP BY NAMEOFTHEBANK




No comments:

Post a Comment