PERCENT RANK and CUME DIST

suggest change

PERCENT_RANK is (rank - 1) / (number of rows - 1) within the window order. The first row in that order is always 0. The last is always 1. With ORDER BY ... DESC, the largest value is first and gets 0. Ties share a rank, so percent rank is not simply “the percentage of rows with a smaller value.”


The CUME_DIST function calculates the relative position of a specified value in a group of values, by determining the percentage of values less than or equal to that value. This is called the cumulative distribution.

SELECT BusinessEntityID, JobTitle, SickLeaveHours,
PERCENT_RANK() OVER(PARTITION BY JobTitle ORDER BY SickLeaveHours DESC)
       AS "Percent Rank",
CUME_DIST() OVER(PARTITION BY JobTitle ORDER BY SickLeaveHours DESC)
       AS "Cumulative Distribution"
FROM Employee;

PARTITION BY groups the rows by job title. ORDER BY sorts each group. CUME_DIST is the fraction of rows at or before the current row in that order. It is not, under DESC, the fraction of numeric values that are smaller.

BusinessEntityID | JobTitle |SickLeaveHours|Percent Rank|Cumulative Distribution|
— | —— |—–|––|––|
267|Application Specialist|57|0|0.25|
268|Application Specialist|56|0.333333333333333|0.75|
269|Application Specialist|56|0.333333333333333|0.75|
272|Application Specialist|55|1|1|
262|Assistant to the Chief Financial Officer|48|0|1|
239|Benefits Specialist|45|0|1|
252|Buyer|50|0|0.111111111111111|
251|Buyer|49|0.125|0.333333333333333|
256|Buyer|49|0.125|0.333333333333333|
253|Buyer|48|0.375|0.555555555555555|
254|Buyer|48|0.375|0.555555555555555|

Within each job title, PERCENT_RANK follows the window order, not the raw numeric order. CUME_DIST includes the current row’s ties, which is why the two Application Specialists with 56 hours both have cumulative distribution 0.75.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents