Aggregation functions
APPROX_COUNT_DISTINCT
Reference material for APPROX_COUNT_DISTINCT
Counts the approximate number of unique values that are not NULL.
Internally, APPROX_COUNT_DISTINCT
uses HyperLogLog (HLL) sketches.
Calling APPROX_COUNT_DISTINCT
returns the same value as calling HLL_COUNT_DISTINCT
with precision 17.
Syntax
Parameters
Parameter | Description | Supported input types |
---|---|---|
<expression> | Expression on which to approximate the distinct count | Any type |
Return Type
BIGINT
Example
When aggregating on few distinct values, APPROX_COUNT_DISTINCT
has no estimation error and returns exact results:
Returns:
approximate | exact |
---|---|
1,000 | 1,000 |
NULL
values do not change the result of APPROX_COUNT_DISTINCT
:
Returns:
approximate | exact |
---|---|
1,000 | 1,000 |
As the number of distinct values grows, the result becomes an approximation:
Returns:
approximate | exact |
---|---|
50,160 | 50,000 |
APPROX_COUNT_DISTINCT
also works for compound types:
Returns:
approximate | exact |
---|---|
4 | 4 |