| 12345678910111213141516171819202122232425262728293031323334353637383940414243 | [[esql-agg-count-distinct]]=== `COUNT_DISTINCT`The approximate number of distinct values.[source.merge.styled,esql]----include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct-result]|===Can take any field type as input and the result is always a `long` not matterthe input type.==== Counts are approximateComputing exact counts requires loading values into a set and returning itssize. This doesn't scale when working on high-cardinality sets and/or largevalues as the required memory usage and the need to communicate thoseper-shard sets between nodes would utilize too many resources of the cluster.This `COUNT_DISTINCT` function is based on thehttps://static.googleusercontent.com/media/research.google.com/fr//pubs/archive/40671.pdf[HyperLogLog++]algorithm, which counts based on the hashes of the values with some interestingproperties:include::../../aggregations/metrics/cardinality-aggregation.asciidoc[tag=explanation]==== Precision is configurableThe `COUNT_DISTINCT` function takes an optional second parameter to configure theprecision discussed previously.[source.merge.styled,esql]----include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct-precision]----[%header.monospaced.styled,format=dsv,separator=|]|===include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct-precision-result]|===
 |