> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> 计算数值数据序列的近似分位数。

# quantile

<div id="quantile">
  ## quantile
</div>

Introduced in：v1.1.0

计算数值数据序列的近似 [`quantile`](https://en.wikipedia.org/wiki/Quantile)。

此函数采用[水库采样](https://en.wikipedia.org/wiki/Reservoir_sampling)，水库大小最高为 8192，并使用随机数生成器进行采样。
结果是非确定性的。
如需获得精确的 分位数，请使用 [`quantileExact`](/docs/zh/reference/functions/aggregate-functions/quantileExact#quantileExact) 函数。

在同一查询中使用多个 level 不同的 `quantile*` 函数时，它们的内部状态不会合并 (也就是说，查询效率会低于原本可达到的水平) 。
在这种情况下，请使用 [`quantiles`](/docs/zh/reference/functions/aggregate-functions/quantiles#quantiles) 函数。

请注意，对于空的数值序列，`quantile` 将返回 NaN，而其 `quantile*` 变体则会根据具体变体返回 NaN 或该序列类型的默认值。

**语法**

```sql theme={null}
quantile(level)(expr)
```

**别名**: `median`

**参数**

* `level` — 可选。分位数级别，为 0 到 1 之间的常量浮点数。建议将 `level` 的值设在 `[0.01, 0.99]` 范围内。默认值：0.5。当 `level=0.5` 时，该函数计算中位数。[`Float`](/docs/zh/reference/data-types/float)

**参数项**

* `expr` — 基于列值的表达式，结果应为数值数据类型、`Date`、`DateTime` 或 `DateTime64`。[`(U)Int*`](/docs/zh/reference/data-types/int-uint) 或 [`Int128`](/docs/zh/reference/data-types/int-uint) 或 [`UInt128`](/docs/zh/reference/data-types/int-uint) 或 [`Int256`](/docs/zh/reference/data-types/int-uint) 或 [`UInt256`](/docs/zh/reference/data-types/int-uint) 或 [`Float*`](/docs/zh/reference/data-types/float) 或 [`Decimal*`](/docs/zh/reference/data-types/decimal) 或 [`Date`](/docs/zh/reference/data-types/date) 或 [`DateTime`](/docs/zh/reference/data-types/datetime) 或 [`DateTime64`](/docs/zh/reference/data-types/datetime64)

**返回值**

指定级别的近似分位数。数值参数将产生 `Float64`，而 `Decimal`、`Date`、`DateTime` 和 `DateTime64` 参数会保留其输入格式。[`Float64`](/docs/zh/reference/data-types/float) 或 [`Decimal*`](/docs/zh/reference/data-types/decimal) 或 [`Date`](/docs/zh/reference/data-types/date) 或 [`DateTime`](/docs/zh/reference/data-types/datetime) 或 [`DateTime64`](/docs/zh/reference/data-types/datetime64)

**示例**

**计算分位数**

```sql title=Query theme={null}
CREATE TABLE t (val UInt32) ENGINE = Memory;
INSERT INTO t VALUES (1), (1), (2), (3);

SELECT quantile(val) FROM t;
```

```response title=Response theme={null}
┌─quantile(val)─┐
│           1.5 │
└───────────────┘
```

**另请参阅**

* [median](/docs/zh/reference/functions/aggregate-functions/median)
* [quantiles](/docs/zh/reference/functions/aggregate-functions/quantiles)
