

 Amazon Redshift will no longer support the creation of new Python UDFs starting Patch 198. Existing Python UDFs will continue to function until June 30, 2026. For more information, see the [ blog post ](https://amazonaws-china.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

# PERCENTILE\$1DISC window function
<a name="r_WF_PERCENTILE_DISC"></a>

PERCENTILE\$1DISC 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\$1DISC 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
<a name="r_WF_PERCENTILE_DISC-synopsis"></a>

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

## Arguments
<a name="r_WF_PERCENTILE_DISC-arguments"></a>

 *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
<a name="r_WF_PERCENTILE_DISC-returns"></a>

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

## Examples
<a name="r_WF_PERCENTILE_DISC-examples"></a>

The following examples use the WINSALES table. For a description of the WINSALES table, see [Sample table for window function examples](c_Window_functions.md#r_Window_function_example). 

```
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\$1DISC(0.25) and PERCENTILE\$1DISC(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        |
+----------+-----+-----------+
```