Updated on 2026-11-06 GMT+08:00

percentile_approx

Function

The percentile_approx function is used to calculate the approximate percentile, suitable for large data volumes. It first sorts the specified column in ascending order, then returns the value closest to the p-th percentile.

Syntax

percentile_approx(DOUBLE col, p [, B])

Parameters

Parameter

Mandatory

Description

col

Yes

A column with numeric data type. Other types return NULL.

p

Yes

0 <= P <= 1, otherwise returns NULL.

B

No

Parameter B controls the approximation accuracy. The larger the B value, the higher the accuracy. The default value is 10000. When the number of distinct values in the column is less than B, the exact percentile is returned.

Return Values

Returns a value of type DOUBLE.

Examples

  • Calculate the 0.5 percentile of all items inventory with accuracy 100. Example command: Assume the elements in column int_test are 1, 2, 3, 4, of type INT.
    select PERCENTILE_APPROX(items,0.5,100) from warehouse; 

    Result:

    +------------+
    | _c0        |
    +------------+
    | 10        |
    +------------+
  • Used with GROUP BY to group all items by warehouse (warehouseId) and calculate the 0.5 percentile of items inventory within the same group with accuracy 100. Example command:
    select warehouseId, PERCENTILE_APPROX(items, 0.5, 100) from warehouse group by warehouseId; 

    Result:

    +------------+------------+
    | warehouseId| _c1        |
    +------------+------------+
    | city1    | 3     |
    | city2    | 20     |
    | city3    | 200     |
    +------------+------------+