- Categories:
Aggregate functions (Percentile Estimation) , Window functions
APPROX_ PERCENTILE¶
Returns an approximated value for the desired percentile (that is, if column c has n numbers,
APPROX_PERCENTILE(c, p) returns a number such that approximately n * p of the numbers in c
are smaller than the returned number).
This function uses an improved version of the t-Digest algorithm. For more information, see Estimating Percentile Values.
Syntax¶
Aggregate function
Window function
Arguments¶
exprA valid expression, such as a column name, that evaluates to a numeric value.
percentileA constant real value greater than or equal to
0.0and less than1.0. This indicates the percentile (from 0 to 99.999…). For example, the value 0.65 indicates the 65th percentile.expr3This is the optional expression used to group rows into partitions.
Returns¶
The output is returned as a DOUBLE value.
Usage notes¶
- Percentile works only on numeric values, so
exprshould produce values that are numbers or can be cast to numbers. - The values returned are not necessarily in the data set.
- The value returned is an approximation. The size of the data set and the skew in the data set affect the accuracy of the approximation.
- When this function is called as a window function, it does not support:
- An ORDER BY clause within the OVER clause.
- Explicit window frames.
- Decimal-float (DECFLOAT) values aren’t supported.
Examples¶
Demonstrate the APPROX_PERCENTILE function:
Create and populate a table with values:
Run queries and show the output:
Note that the value returned in this case is higher than any value actually in the data set: