Updated on 2026-11-06 GMT+08:00

percent_rank

Function

The percent_rank function is used to return the value of the column specified in the ORDER BY clause of the window, but expressed as a decimal between 0 and 1. The calculation method is (RANK value of the current row within the group – 1)/(Total number of rows within the group – 1).

Usage Restrictions

The usage restrictions for window functions are as follows:

  • Window functions can only appear in SELECT statements.
  • Window functions cannot be nested with other window functions or aggregate functions.
  • Window functions cannot be used together with aggregate functions at the same level.

Syntax

percent_rank() over([partition_clause] [orderby_clause])

Parameters

Parameter

Mandatory

Description

partition_clause

No

Specifies the partition. Rows with the same value in the partition column are considered to be within the same window.

orderby_clause

No

Specifies how data is sorted within a window.

Return Values

Returns a value of type DOUBLE.

Examples

To facilitate understanding of the function's usage, source data is provided along with function-related examples. Create the salary table and add data with the following example command:

CREATE EXTERNAL TABLE salary (
dept STRING, -- <department-name>
userid string, -- <employee-ID>
sal INT -- <salary>
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' 
stored as textfile;

Add data as follows:

d1,user1,1000 
d1,user2,2000 
d1,user3,3000 
d2,user4,4000 
d2,user5,5000

Example: Calculate the percentage rank of employee salaries within their departments.

select dept, userid, sal,
       percent_rank() over(partition by dept order by sal) as pr2
from salary;
-- Result analysis:
d1 user1 1000 0.0    -- (1-1)/(3-1)=0.0
d1 user2 2000 0.5    -- (2-1)/(3-1)=0.5
d1 user3 3000 1.0    -- (3-1)/(3-1)=1.0
d2 user4 4000 0.0    -- (1-1)/(2-1)=0.0
d2 user5 5000 1.0    -- (2-1)/(2-1)=1.0