PERCENT_RANK
Description
The PERCENT_RANK
function is used to calculate the percentage rank of a specified row within its partition. This function sorts the data according to the sorting order specified by the ORDER BY
clause and calculates the rank of each row within each partition. The return value is of type double
, ranging from [0.0, 1.0]. The specific calculation formula is (current row rank - 1) / (total number of rows in the partition - 1)
. When there is only one row in the partition, the return value is 0.
Syntax
PARTITION BY
clause (optional): Specifies the expression(s) used for partitioning, multiple expressions can be used. If this clause is omitted, the entire result set will be used as a single partition.ORDER BY
clause: Specifies the expression(s) used for sorting, multiple expressions can be used, and the sorting direction (ascending ASC or descending DESC) can be specified.
Example
The following example demonstrates how to use the PERCENT_RANK
function to calculate the salary rank percentage of employees in different departments.