PERCENTILE DISC and PERCENTILE CONT

suggest change

PERCENTILE_DISC returns the first value in the window order whose cumulative distribution is greater than or equal to the percentile you pass.

The values are grouped by rowset or partition, as specified by the WITHIN GROUP clause.


PERCENTILE_CONT interpolates between two surrounding values instead of returning a value that occurs in the column. It is not the average of a sum.

SELECT BusinessEntityID, JobTitle, SickLeaveHours,
       CUME_DIST() OVER(PARTITION BY JobTitle ORDER BY SickLeaveHours ASC)
       AS "Cumulative Distribution",
       PERCENTILE_DISC(0.5) WITHIN GROUP(ORDER BY SickLeaveHours)
          OVER(PARTITION BY JobTitle) AS "Percentile Discrete"
FROM Employee;

To find the exact value from the row that matches or exceeds the 0.5 percentile, you pass the percentile as the numeric literal in the PERCENTILE_DISC function. The Percentile Discrete column lists the smallest value whose cumulative distribution is greater than or equal to the given percentile.

BusinessEntityID | JobTitle |SickLeaveHours|Cumulative Distribution|Percentile Discrete|
— | —— |—–|––|––|
272|Application Specialist|55|0.25|56|
268|Application Specialist|56|0.75|56|
269|Application Specialist|56|0.75|56|
267|Application Specialist|57|1|56|


PERCENTILE_CONT interpolates. For these four hours, the 0.5 percentile falls halfway between the second and third values, which are both 56, so the continuous result is 56.

SELECT BusinessEntityID, JobTitle, SickLeaveHours,
       CUME_DIST() OVER(PARTITION BY JobTitle ORDER BY SickLeaveHours ASC)
       AS "Cumulative Distribution",
       PERCENTILE_DISC(0.5) WITHIN GROUP(ORDER BY SickLeaveHours) 
          OVER(PARTITION BY JobTitle) AS "Percentile Discrete",
       PERCENTILE_CONT(0.5) WITHIN GROUP(ORDER BY SickLeaveHours) 
          OVER(PARTITION BY JobTitle) AS "Percentile Continuous"
FROM Employee;

BusinessEntityID | JobTitle |SickLeaveHours|Cumulative Distribution|Percentile Discrete|Percentile Continuous|
— | —— |—–|––|––|—|
272|Application Specialist|55|0.25|56|56|
268|Application Specialist|56|0.75|56|56|
269|Application Specialist|56|0.75|56|56|
267|Application Specialist|57|1|56|56|

Feedback about page:

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



Table Of Contents