Categories:

Aggregate Functions (Cardinality Estimation) , Window Functions

APPROX_COUNT_DISTINCT

Uses HyperLogLog to return an approximation of the distinct cardinality of the input.

Alias for HLL.

Syntax

APPROX_COUNT_DISTINCT( [ DISTINCT ] <expr>  [ , ... ] )

APPROX_COUNT_DISTINCT(*)

Returns

The data type of the returned value is INTEGER.

Examples

This example shows how to use APPROX_COUNT_DISTINCT(). This example calls both COUNT(DISTINCT i) and APPROX_COUNT_DISTINCT(i) to emphasize that the results do not always match exactly.

SELECT COUNT(i), COUNT(DISTINCT i), APPROX_COUNT_DISTINCT(i)
  FROM sequence_demo;

Output:

The results might vary since APPROX_COUNT_DISTINCT() returns an approximation, not an exact value.

+----------+-------------------+--------------------------+
| COUNT(I) | COUNT(DISTINCT I) | APPROX_COUNT_DISTINCT(I) |
|----------+-------------------+--------------------------|
|     1024 |              1024 |                     1030 |
+----------+-------------------+--------------------------+