> ## 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.

> 计算不同参数值的近似数量。

# uniqCombined

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

引入版本：v1.1.0

计算不同参数值的近似数量。
该函数会以确定性的方式返回结果 (即不依赖于查询处理顺序) 。

<Note>
  由于它对非 `String` 类型使用 32 位哈希，因此当基数明显大于 `UINT_MAX` 时，结果会出现非常大的误差 (在不同值达到几百亿后，误差会迅速增大) 。
  如果基数大于 `UINT_MAX`，则应改用 [`uniqCombined64`](/zh/reference/functions/aggregate-functions/uniqCombined64)。
</Note>

与 uniq 函数相比，uniqCombined 函数：

* 内存消耗低数倍
* 计算精度高数倍
* 通常性能会略低一些。但在某些场景下，uniqCombined 的表现可能优于 uniq，例如在分布式查询中需要通过网络传输大量聚合状态时

<Accordion title="实现细节">
  此函数会先对聚合中的所有参数计算哈希值 (`String` 使用 64 位哈希，其他类型使用 32 位哈希) ，然后在计算中使用该哈希值。
  它结合了三种算法：数组、哈希表，以及带误差修正表的 HyperLogLog：

  * 当不同元素较少时，使用数组
  * 当集合大小进一步增大时，使用哈希表
  * 当元素更多时，使用 HyperLogLog，此时会占用固定大小的内存
</Accordion>

**语法**

```sql theme={null}
uniqCombined(HLL_precision)(x[, ...])
uniqCombined(x[, ...])
```

**参数**

* `HLL_precision` — 可选。HyperLogLog 中单元数量以 2 为底的对数。默认值为 17，实际占用空间约为 96 KiB (2^17 个单元，每个单元 6 位) 。范围：\[12, 20]。[`UInt8`](/zh/reference/data-types/int-uint)

**实参**

* `x` — 数量可变的参数。[`Tuple(T)`](/zh/reference/data-types/tuple) 或 [`Array(T)`](/zh/reference/data-types/array) 或 [`Date`](/zh/reference/data-types/date) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`String`](/zh/reference/data-types/string) 或 [`(U)Int*`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Decimal`](/zh/reference/data-types/decimal)

**返回值**

返回一个 `UInt64` 类型的值，表示不同参数值的近似数量。[`UInt64`](/zh/reference/data-types/int-uint)

**示例**

**基本用法**

```sql title=Query theme={null}
SELECT uniqCombined(number) FROM numbers(1e6);
```

```response title=Response theme={null}
┌─uniqCombined(number)─┐
│              1001148 │
└──────────────────────┘
```

**使用自定义精度**

```sql title=Query theme={null}
SELECT uniqCombined(15)(number) FROM numbers(1e5);
```

```response title=Response theme={null}
┌─uniqCombined(15)(number)─┐
│                   100768 │
└──────────────────────────┘
```

**另见**

* [uniq](/zh/reference/functions/aggregate-functions/uniq)
* [uniqCombined64](/zh/reference/functions/aggregate-functions/uniqCombined64)
* [uniqHLL12](/zh/reference/functions/aggregate-functions/uniqHLL12)
* [uniqExact](/zh/reference/functions/aggregate-functions/uniqExact)
* [uniqTheta](/zh/reference/functions/aggregate-functions/uniqthetasketch)
