CUME_DIST
Returns the share of rows of a window partition that come before the current row or together with it, from 0 to 1.
PostgreSQL 17.5
CUME_DIST() OVER ([PARTITION BY ...] [ORDER BY ...])Parameters
- PARTITION BYoptional
- Columns that split rows into partitions, shares are computed in each one separately
- ORDER BYoptional
- Order of rows used to compute the share
Return value
A floating-point number (double precision) greater than 0 and up to 1
Examples
PERCENT_RANK and CUME_DIST
PostgreSQL 17.5
SELECT unit_price,
ROUND(
PERCENT_RANK() OVER (
ORDER BY unit_price
)::numeric,
3
) AS pct_rank,
ROUND(
CUME_DIST() OVER (
ORDER BY unit_price
)::numeric,
3
) AS cume_dist
FROM Payments
ORDER BY unit_price;Share of payments not more expensive
PostgreSQL 17.5
SELECT family_member,
unit_price,
ROUND(
CUME_DIST() OVER (
PARTITION BY family_member
ORDER BY unit_price
)::numeric,
2
) AS share_not_more
FROM Payments
ORDER BY family_member,
unit_price;Details
It is computed as the number of rows with an ORDER BY value not greater than the current one, divided by the number of rows in the partition. Unlike PERCENT_RANK, the result is never 0, and for the last row it is always 1.