Saturday 20 July 2024

PERCENT_Rank() function in SQL server

It is used to calculate the relative rank of specific row with in the result set. It always return values greater than 0 and less than or equal to 1. It does not count any null value. This function is non- deterministic. Percent rank will be always 0 for the first row.  

Below formula used inside this function. 

Syntax of Percent Rank 

Below is the example

SELECT COUNT(*) NumberOfOrders, Month(OrderDate) AS OrderMonth,

RANK() OVER(ORDER BY COUNT(*)) AS Ranking,

PERCENT_RANK() OVER(ORDER BY COUNT(*)) AS PercentRank

FROM Sales.SalesOrderHeader

WHERE OrderDate >= '2013-01-01' AND OrderDate < '2014-01-01'

GROUP BY Month(OrderDate);

 


No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts