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

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

# uniq

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

Introduced in: v1.1.0

计算参数中不同值的近似个数。

该函数使用自适应采样算法。在计算状态中，函数会使用最多 65536 个元素哈希值的样本。该算法精度很高，且 CPU 效率也很高。当查询中包含多个此类函数时，使用 uniq 的速度几乎与使用其他聚合函数一样快。

<Accordion title="Implementation details">
  该函数会先对聚合中的所有参数计算哈希值，再将其用于计算。
  它使用自适应采样算法。
  在计算状态中，函数会使用最多 65536 个元素哈希值的样本。
  该算法精度很高，且 CPU 效率也很高。
  当查询中包含多个此类函数时，使用 `uniq` 的速度几乎与使用其他聚合函数一样快。
</Accordion>

<Tip>
  在几乎所有场景下，我们都建议优先使用此函数而非其他变体。
</Tip>

**Syntax**

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

**参数**

* `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}
CREATE TABLE example_table (
    id UInt32,
    category String,
    value Float64
) ENGINE = Memory;

INSERT INTO example_table VALUES
(1, 'A', 10.5),
(2, 'B', 20.3),
(3, 'A', 15.7),
(4, 'C', 8.9),
(5, 'B', 12.1),
(6, 'A', 18.4);

SELECT uniq(category) as unique_categories
FROM example_table;
```

```response title=Response theme={null}
┌─unique_categories─┐
│                 3 │
└───────────────────┘
```

**多个参数**

```sql title=Query theme={null}
SELECT uniq(category, value) as unique_combinations
FROM example_table;
```

```response title=Response theme={null}
┌─unique_combinations─┐
│                   6 │
└─────────────────────┘
```

**另请参阅**

* [uniqCombined](/zh/reference/functions/aggregate-functions/uniqCombined)
* [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)
