Function Reference

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;
unit_pricepct_rankcume_dist
70.0000.071
70.0000.071
80.0740.179
80.0740.179
80.0740.179
100.1850.214
160.2220.250
200.2590.286
590.2960.321
1000.3330.393

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;
family_memberunit_priceshare_not_more
170.25
170.25
180.50
180.50
1100.63
13000.75
120000.88
1100001.00
280.14
21200.29

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.

See also