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

cume_dist

Function

The cume_dist function is used to calculate the cumulative distribution, which is equivalent to calculating the proportion of data greater than or equal to, or less than or equal to the current row within a partition.

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

cume_dist() 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 
  • Calculate the proportion of people with salaries less than or equal to the current salary.
    select dept, userid, sal,
            cume_dist() over(order by sal) as cume1 
    from salary; 
    --  Result: 
    d1 user1 1000 0.2 
    d1 user2 2000 0.4 
    d1 user3 3000 0.6 
    d2 user4 4000 0.8 
    d2 user5 5000 1.0
  • Group by department and calculate the proportion of employees with salaries less than or equal to the current salary within each department.
    select dept, userid, sal,
            cume_dist() over (partition by dept order by sal) as cume2 
    from salary; 
    -- Result: 
    d1 user1 1000 0.3333333333333333 
    d1 user2 2000 0.6666666666666666 
    d1 user3 3000 1.0 
    d2 user4 4000 0.5 
    d2 user5 5000 1.0
  • After sorting by sal in descending order, the result calculates the proportion of employees with salaries greater than or equal to the current salary.
    select dept, userid, sal,
            cume_dist() over(order by sal desc) as cume3 
    from salary;
     -- Result:
    d2 user5 5000 0.2 
    d2 user4 4000 0.4 
    d1 user3 3000 0.6 
    d1 user2 2000 0.8 
    d1 user1 1000 1.0 select dept, userid, sal,
            cume_dist() over(partition by dept order by sal desc) as cume4 
    from salary;
     -- Result: 
    d1 user3 3000 0.3333333333333333 
    d1 user2 2000 0.6666666666666666 
    d1 user1 1000 1.0 
    d2 user5 5000 0.5 
    d2 user4 4000 1.0