View a markdown version of this page

Median and percentile functions - Amazon Athena

Median and percentile functions

Use these functions to find the median or another quantile, such as the 90th percentile, of the values in a group. Some return an exact value, and others return an estimate computed from a summary or a sample of the values.

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

approx_quantile function

Returns an estimate of the value at quantile pos of the non-NULL values of x, computed with the t-digest algorithm. For example, a pos of 0.5 estimates the median and a pos of 0.9 estimates the 90th percentile. With pos_list, the function returns a list that has one estimate for each quantile, in the order of pos_list.

The estimate has the type of x, except that with a single pos, TINYINT input returns a BIGINT and FLOAT input returns a DOUBLE. For integer input, the result is an integer, and for DECIMAL input it keeps the scale of x, so a fractional estimate can be rounded. To keep the fraction, cast x to DOUBLE. If every value is NULL, or if there are no input rows, the result is NULL.

Syntax

approx_quantile(x, pos) approx_quantile(x, pos_list)

Arguments

The approx_quantile function takes the following arguments:

x

The values to estimate the quantile of. A numeric value, such as INTEGER, DOUBLE, or DECIMAL, or a DATE, TIME, or TIMESTAMP value. VARCHAR values are not accepted.

pos

The quantile to estimate, as a constant FLOAT from 0 to 1.

pos_list

The quantiles to estimate, as a constant list of FLOAT values from 0 to 1, such as [0.25, 0.5, 0.75].

Return type

The type of x, such as INTEGER for INTEGER input or DECIMAL(2,1) for DECIMAL(2,1) input. With pos_list, a list of that type, such as DOUBLE[] for DOUBLE input. With a single pos, TINYINT input returns BIGINT and FLOAT input returns DOUBLE.

Examples

The following SQL query estimates the median of four DECIMAL values.

SELECT approx_quantile(x, 0.5) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

result

2.5

The following SQL query estimates three quantiles at once. The values are cast to DOUBLE, so the estimates keep their fractions.

SELECT approx_quantile( CAST(x AS DOUBLE), [0.25, 0.5, 0.75] ) AS result FROM (VALUES (1), (2), (3), (4), (5)) AS v(x)

The query returns the following result.

result

[1.75, 3.0, 4.25]

The following SQL query shows that integer input returns an integer. The median of 1 to 4 is 2.5, but the INTEGER result has no fraction. Casting to DOUBLE keeps it.

SELECT approx_quantile(x, 0.5) AS integer_result, approx_quantile( CAST(x AS DOUBLE), 0.5 ) AS double_result FROM (VALUES (1), (2), (3), (4)) AS v(x)

The query returns the following result.

integer_result double_result

2

2.5

Considerations

pos and pos_list must be constants from 0 to 1. A value outside that range, or a column reference, makes the query fail.

The result is an estimate. For exact quantiles, use the quantile_cont function or the quantile_disc function.

Differences from Athena engine version 3

Athena engine version 3 names this function approx_percentile, and DuckDB has no approx_percentile function. Replace approx_percentile(x, p) with approx_quantile(x, p), and approx_percentile(x, ARRAY[p1, p2]) with approx_quantile(x, [p1, p2]). For exact values, use the quantile_cont function or the quantile_disc function.

median function

Returns the median of x, the value in the middle when the values are sorted. When the group has an even number of values, the result for numbers, dates, and times is the average of the two middle values. For other types, such as VARCHAR, it is the smaller of the two middle values.

NULL values are ignored. If the group has no values that are not NULL, the result is NULL. For numbers, dates, and times, median(x) returns the same result as quantile_cont(x, 0.5). To get one of the input values instead of an average, use quantile_disc(x, 0.5).

Syntax

median(x)

Arguments

The median function takes the following argument:

x

The values, of any type that can be ordered.

Return type

DOUBLE for integer input, TIMESTAMP for DATE input, and the type of x for other types, including DECIMAL.

Examples

The following SQL query returns the median of four values, which is the average of the two middle values.

SELECT median(x) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

result

2.5

The following SQL query returns the median of four integers and of four strings. For the integers, the result is the average of 2 and 3, as a DOUBLE. Strings cannot be averaged, so the result is the smaller of the two middle strings.

SELECT median(n) AS median_number, median(s) AS median_string FROM (VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry'), (4, 'date') ) AS v(n, s)

The query returns the following result.

median_number median_string

2.5

banana

The following SQL query shows that the median of two DECIMAL(2,1) values keeps one decimal place and drops the rest. Cast to DOUBLE to get the exact average.

SELECT median(x) AS decimal_result, median(CAST(x AS DOUBLE)) AS double_result FROM (VALUES (1.0), (2.5)) AS v(x)

The query returns the following result.

decimal_result double_result

1.7

1.75

The following SQL query returns the median order amount in each region of a table. The order with a NULL amount is ignored.

SELECT region, median(amount) AS median_amount FROM sales_db.sales_parquet GROUP BY region ORDER BY region

The query returns the following results.

region median_amount

east

81.37

north

310.00

west

46.87

Considerations

For a DECIMAL input, the result has the same scale as x, and the digits past that scale are dropped, not rounded. For example, the median of 1.0 and 2.5 is 1.7, not 1.75. To keep those digits, cast x to DOUBLE or to a DECIMAL with a larger scale.

quantile_cont function

Returns the value at the fraction pos of the way through the sorted values of x. When that position falls between two values, the function interpolates linearly between them, so the result can be a value that does not occur in the data. For example, quantile_cont(x, 0.5) returns the median, and quantile_cont(x, 0.9) returns the 90th percentile.

To get several quantiles at once, pass a list of positions, such as [0.25, 0.5, 0.75]. The result is then a list with one quantile for each position. NULL values of x are ignored. If the group has no values that are not NULL, the result is NULL. To get a quantile that is always one of the input values, use the quantile_disc function.

Syntax

quantile_cont(x, pos) quantile_cont(x, pos_list)

Arguments

The quantile_cont function takes the following arguments:

x

The values to find the quantile of, as a numeric value, or a DATE, TIME, TIME WITH TIME ZONE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

pos

The position of the quantile, as a constant DOUBLE from 0 to 1. It cannot be NULL or a column.

pos_list

The positions of the quantiles, as a constant list in which each position is a DOUBLE from 0 to 1, such as [0.25, 0.5, 0.75]. The result is a list with one quantile for each position, in the same order.

Return type

DOUBLE for integer input, TIMESTAMP for DATE input, and the type of x for other types, including DECIMAL. With pos_list, a LIST of that type.

Examples

The following SQL query returns the median of four values. The position falls between 2.0 and 3.0, so the result is interpolated.

SELECT quantile_cont(x, 0.5) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

result

2.5

The following SQL query returns the quartiles of four integers as a list of DOUBLE values.

SELECT quantile_cont(x, [0.25, 0.5, 0.75]) AS result FROM (VALUES (1), (2), (3), (4)) AS v(x)

The query returns the following result.

result

[1.75, 2.5, 3.25]

The following SQL query shows that a DECIMAL(2,1) input gives a result with one decimal place: the interpolated value 1.75 becomes 1.7. Cast to DOUBLE to keep every digit.

SELECT quantile_cont(x, 0.25) AS decimal_result, quantile_cont( CAST(x AS DOUBLE), 0.25 ) AS double_result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

decimal_result double_result

1.7

1.75

The following SQL query uses the SQL-standard form percentile_cont(pos) WITHIN GROUP (ORDER BY x), which returns the same result as quantile_cont(x, pos).

SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY x) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

result

2.5

Considerations

For a DECIMAL input, the result has the same scale as x, and the digits past that scale are dropped, not rounded. To keep those digits, cast x to DOUBLE or to a DECIMAL with a larger scale.

If pos or an element of pos_list is NULL, is not a constant, or is greater than 1, the query fails.

Differences from Athena engine version 3

Athena engine version 3 has no quantile_cont function, and its approx_percentile function is not available in DuckDB. To replace approx_percentile(x, p), use approx_quantile(x, p) for an approximate result, or quantile_cont or quantile_disc for an exact result.

quantile_disc function

Returns a quantile of x without interpolation: the smallest value for which at least the fraction pos of the values are less than or equal to it. The result is always one of the input values, so quantile_disc works on any type that can be ordered, including VARCHAR.

To get several quantiles at once, pass a list of positions, such as [0.25, 0.5, 0.75]. The result is then a list with one quantile for each position. NULL values of x are ignored. If the group has no values that are not NULL, the result is NULL. To interpolate between values instead, use the quantile_cont function.

quantile is an alias of quantile_disc.

Syntax

quantile_disc(x, pos) quantile_disc(x, pos_list)

Arguments

The quantile_disc function takes the following arguments:

x

The values, of any type that can be ordered.

pos

The position of the quantile, as a constant DOUBLE from 0 to 1. It cannot be NULL or a column.

pos_list

The positions of the quantiles, as a constant list in which each position is a DOUBLE from 0 to 1, such as [0.25, 0.5, 0.75]. The result is a list with one quantile for each position, in the same order.

Return type

The type of x. With pos_list, a LIST of the type of x.

Examples

The following SQL query returns the median of four values. With an even number of values, the result is the smaller of the two middle values.

SELECT quantile_disc(x, 0.5) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

result

2.0

The following SQL query uses the quantile alias to return the quartiles of four integers as a list.

SELECT quantile(x, [0.25, 0.5, 0.75]) AS result FROM (VALUES (10), (20), (30), (40)) AS v(x)

The query returns the following result.

result

[10, 20, 30]

The following SQL query returns the median of four strings. With an even number of values, the result is the smaller of the two middle strings, banana.

SELECT quantile_disc(s, 0.5) AS result FROM (VALUES ('apple'), ('banana'), ('cherry'), ('date') ) AS v(s)

The query returns the following result.

result

banana

The following SQL query uses the SQL-standard form percentile_disc(pos) WITHIN GROUP (ORDER BY x), which returns the same result as quantile_disc(x, pos).

SELECT percentile_disc(0.5) WITHIN GROUP (ORDER BY x) AS result FROM (VALUES (1.0), (2.0), (3.0), (4.0)) AS v(x)

The query returns the following result.

result

2.0

Considerations

If pos or an element of pos_list is NULL, is not a constant, or is greater than 1, the query fails.

Differences from Athena engine version 3

Athena engine version 3 has no quantile_disc function, and its approx_percentile function is not available in DuckDB. To replace approx_percentile(x, p), use approx_quantile(x, p) for an approximate result, or quantile_disc or quantile_cont for an exact result.

reservoir_quantile function

Returns the value at position quantile in the sorted values of x, where 0 is the smallest value, 0.5 the median, and 1 the largest. The function keeps a random sample, called a reservoir, of at most sample_size values, and finds the quantile in that sample. When the group has more values than sample_size, the result is an estimate. When it has sample_size values or fewer, the sample holds every value, so the result does not depend on sampling.

The result is always one of the input values: the function does not interpolate between two values. For an even number of values and a quantile of 0.5, it returns the lower of the two middle values. To get several quantiles at once, pass a list of quantiles. The result is a list with one value for each quantile, in the same order.

NULL values are ignored. If every value is NULL, or the group has no rows, the result is NULL.

Syntax

reservoir_quantile(x [, quantile [, sample_size ] ]) reservoir_quantile(x, quantiles [, sample_size ])

Arguments

The reservoir_quantile function takes the following arguments:

x

The values, of a numeric type: TINYINT, SMALLINT, INTEGER, BIGINT, HUGEINT, FLOAT, DOUBLE, or DECIMAL. Other types, such as DATE and VARCHAR, are not accepted.

quantile

The position of the quantile, as a constant DOUBLE from 0 to 1. The default is 0.5, the median. This argument is optional.

quantiles

The positions of the quantiles, as a constant list of DOUBLE values, each from 0 to 1, such as [0.25, 0.5, 0.75].

sample_size

The largest number of values that the sample keeps, as a constant INTEGER greater than 0. The default is 8192. This argument is optional.

Return type

The type of x, for example INTEGER for INTEGER input and DECIMAL(2,1) for DECIMAL(2,1) input. With a list of quantiles, a list of that type, such as INTEGER[].

Examples

The following SQL query returns the median of five values, with a sample size of 1024. The group has fewer values than the sample size, so the result is the exact median.

SELECT reservoir_quantile(x, 0.5, 1024) AS result FROM (VALUES (1), (2), (3), (4), (5)) AS t(x)

The query returns the following result.

result

3

The following SQL query returns the values at the quantiles 0.25, 0.5, and 0.75 as a list. The function does not interpolate, so the value for 0.5 is 4, the lower of the two middle values.

SELECT reservoir_quantile(x, [0.25, 0.5, 0.75]) AS result FROM (VALUES (1), (2), (3), (4), (5), (6), (7), (8) ) AS t(x)

The query returns the following result.

result

[2, 4, 6]

The following SQL query has a NULL, which both functions ignore, so each finds the median of the other four values. The reservoir_quantile function returns the lower of the two middle values, and median returns their average.

SELECT reservoir_quantile(x, 0.5) AS reservoir_median, median(x) AS interpolated_median FROM (VALUES (10), (20), (NULL), (30), (40)) AS t(x)

The query returns the following result.

reservoir_median interpolated_median

20

25.0

Considerations

quantile, quantiles, and sample_size must be constants. The query fails if you pass a column, a quantile outside the range 0 to 1, or a sample size of 0 or less.

On a group with more values than sample_size, the result can differ from the exact quantile. For an exact result, use the quantile_disc function, which also returns one of the input values, or quantile_cont, which interpolates between values.