> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 计算数据集的样本方差。

# varSamp

<h2 id="varSamp">
  varSamp
</h2>

引入版本：v1.1.0

计算数据集的样本方差。

样本方差的计算公式如下：

$$
\frac{\Sigma{(x - \bar{x})^2}}{n-1}
$$

<br />

其中：

* $x$ 为数据集中的每个数据点
* $\bar{x}$ 为数据集的算术平均值
* $n$ 为数据集中的数据点数量

该函数假设输入数据集是从更大总体中抽取的样本。若需要计算整个总体的方差（即已有完整数据集时），请改用 [`varPop`](/reference/functions/aggregate-functions/varPop)。

<Note>
  该函数使用数值不稳定的算法。如果计算中需要[数值稳定性](https://en.wikipedia.org/wiki/Numerical_stability)，请使用 [`varSampStable`](/reference/functions/aggregate-functions/varSampStable) 函数。该函数运行速度较慢，但计算误差更小。
</Note>

**语法**

```sql theme={null}
varSamp(x)
```

**别名**：`VAR_SAMP`

**参数**

* `x` — 需要计算样本方差的数据集。类型为 [`(U)Int*`](/reference/data-types/int-uint)、[`Float*`](/reference/data-types/float) 或 [`Decimal*`](/reference/data-types/decimal)

**返回值**

返回输入数据集 `x` 的样本方差。类型为 [`Float64`](/reference/data-types/float)

**示例**

**计算样本方差**

```sql title=Query theme={null}
DROP TABLE IF EXISTS test_data;
CREATE TABLE test_data
(
    x Float64
)
ENGINE = Memory;

INSERT INTO test_data VALUES (10.5), (12.3), (9.8), (11.2), (10.7);

SELECT round(varSamp(x),3) AS var_samp FROM test_data;
```

```response title=Response theme={null}
┌─var_samp─┐
│    0.865 │
└──────────┘
```
