PERCENTILE_DISC window function - Amazon Redshift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

PERCENTILE_DISC window function

PERCENTILE_DISC is an inverse distribution function that assumes a discrete distribution model. It takes a percentile value and a sort specification and returns an element from the given set.

For a given percentile value P, PERCENTILE_DISC sorts the values of the expression in the ORDER BY clause and returns the value with the smallest cumulative distribution value (with respect to the same sort specification) that is greater than or equal to P.

You can specify only the PARTITION clause in the OVER clause.

Syntax

PERCENTILE_DISC ( percentile ) WITHIN GROUP (ORDER BY expr) OVER ( [ PARTITION BY expr_list ] )

Arguments

percentile

Numeric constant between 0 and 1. Nulls are ignored in the calculation.

WITHIN GROUP ( ORDER BY expr)

Specifies numeric or date/time values to sort and compute the percentile over.

OVER

Specifies the window partitioning. The OVER clause cannot contain a window ordering or window frame specification.

PARTITION BY expr

Optional argument that sets the range of records for each group in the OVER clause.

Returns

The same data type as the ORDER BY expression in the WITHIN GROUP clause.

Examples

The following examples use the WINSALES table. For a description of the WINSALES table, see Sample table for window function examples.

SELECT sellerid, qty, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY qty) OVER() AS MEDIAN FROM winsales; +----------+-----+--------+ | sellerid | qty | median | +----------+-----+--------+ | 3 | 10 | 20 | | 1 | 10 | 20 | | 1 | 10 | 20 | | 4 | 10 | 20 | | 3 | 15 | 20 | | 2 | 20 | 20 | | 2 | 20 | 20 | | 3 | 20 | 20 | | 1 | 30 | 20 | | 3 | 30 | 20 | | 4 | 40 | 20 | +----------+-----+--------+ SELECT sellerid, qty, PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY qty) OVER(PARTITION BY sellerid) AS MEDIAN FROM winsales; +----------+-----+--------+ | sellerid | qty | median | +----------+-----+--------+ | 4 | 10 | 10 | | 4 | 40 | 10 | | 3 | 10 | 15 | | 3 | 15 | 15 | | 3 | 20 | 15 | | 3 | 30 | 15 | | 2 | 20 | 20 | | 2 | 20 | 20 | | 1 | 10 | 10 | | 1 | 10 | 10 | | 1 | 30 | 10 | +----------+-----+--------+

To find PERCENTILE_DISC(0.25) and PERCENTILE_DISC(0.75) for the quantity when partitioned by the seller ID, use the following examples.

SELECT sellerid, qty, PERCENTILE_DISC(0.25) WITHIN GROUP (ORDER BY qty) OVER(PARTITION BY sellerid) AS quartile1 FROM winsales; +----------+-----+-----------+ | sellerid | qty | quartile1 | +----------+-----+-----------+ | 4 | 10 | 10 | | 4 | 40 | 10 | | 2 | 20 | 20 | | 2 | 20 | 20 | | 3 | 10 | 10 | | 3 | 15 | 10 | | 3 | 20 | 10 | | 3 | 30 | 10 | | 1 | 10 | 10 | | 1 | 10 | 10 | | 1 | 30 | 10 | +----------+-----+-----------+ SELECT sellerid, qty, PERCENTILE_DISC(0.75) WITHIN GROUP (ORDER BY qty) OVER(PARTITION BY sellerid) AS quartile3 FROM winsales; +----------+-----+-----------+ | sellerid | qty | quartile3 | +----------+-----+-----------+ | 3 | 10 | 20 | | 3 | 15 | 20 | | 3 | 20 | 20 | | 3 | 30 | 20 | | 4 | 10 | 40 | | 4 | 40 | 40 | | 2 | 20 | 20 | | 2 | 20 | 20 | | 1 | 10 | 30 | | 1 | 10 | 30 | | 1 | 30 | 30 | +----------+-----+-----------+