aggregate

PERCENTILE_DISC

Selects the first ordered non-NULL value whose cumulative fraction reaches the requested fraction. Unlike interpolation, the result is an observed value. Example target: PostgreSQL 17; inline synthetic data; no extensions.

Formal Syntax & Structure

sql syntaxsql
PERCENTILE_DISC(fraction) WITHIN GROUP (ORDER BY expression)

Working Example

Example Snippetsql
WITH sample(id,x,y) AS (VALUES (1,1,2),(2,2,4),(3,3,6),(4,4,8),(5,NULL,4)) SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY x) FROM sample;
SQL Reference