## COUNT_DISTINCT

The `COUNT_DISTINCT` function returns the approximate number of distinct values. It can take any field type as input. This function is based on the HyperLogLog++ algorithm, which counts based on the hashes of the values with some interesting properties such as configurable precision, excellent accuracy on low-cardinality sets, and fixed memory usage.

### Syntax

`COUNT_DISTINCT(expression[, precision_threshold])`

#### Parameters

- `expression`: Expression that outputs the values on which to perform a distinct count.
- `precision_threshold`: Precision threshold. The maximum supported value is 40000. Thresholds above this number will have the same effect as a threshold of 40000. The default value is 3000.

### Examples

Here are a couple of examples of how to use the `COUNT_DISTINCT` function in ES|QL queries:

```esql
FROM hosts
| STATS COUNT_DISTINCT(ip0), COUNT_DISTINCT(ip1)
```

In this example, the `COUNT_DISTINCT` function is used to count the distinct values of `ip0` and `ip1` from the `hosts` index.

```esql
FROM hosts
| STATS COUNT_DISTINCT(ip0, 80000), COUNT_DISTINCT(ip1, 5)
```

In this example, the `COUNT_DISTINCT` function is used with an optional second parameter to configure the precision threshold.

```esql
ROW words="foo;bar;baz;qux;quux;foo"
| STATS distinct_word_count = COUNT_DISTINCT(SPLIT(words, ";"))
```

In this example, the `COUNT_DISTINCT` function is used with the `SPLIT` function to count the unique values in a string.