PERCENTILE_DISC
Returns a percentile, the first value of the group at which the cumulative share of values reaches the given one.
PostgreSQL 17.5
PERCENTILE_DISC(fraction) WITHIN GROUP (ORDER BY expression)Parameters
- fractiondouble precision
- Share from 0 to 1, for example 0.5 for the median
- expression
- Expression of any sortable type: a number, string or date
Return value
A value of the same type as expression; NULL for an empty set
Examples
PERCENTILE_CONT and PERCENTILE_DISC
PostgreSQL 17.5
SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (
ORDER BY x
) AS cont,
PERCENTILE_DISC(0.5) WITHIN GROUP (
ORDER BY x
) AS disc
FROM (
VALUES (1),
(2),
(3),
(10)
) AS t(x);Median birth date
PostgreSQL 17.5
SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (
ORDER BY birthday
) AS median_birthday
FROM FamilyMembers;Details
Unlike PERCENTILE_CONT, it always returns one of the values of the set, so it also works for dates and strings. The expression goes in WITHIN GROUP (ORDER BY ...).