APPROX_DISTINCT
All functions > AGGREGATE > APPROX_DISTINCT
Returns the approximate number of distinct values in the group.
Syntax
APPROX_DISTINCT(expr, group_by, where[, precision])
sql
Arguments
Parameter | Type | Required | Description |
---|---|---|---|
expr | BIGINT | Yes | The expression to count distinct values of |
precision | BIGINT | No | Optional precision parameter for accuracy control |
Returns
Approximate count of distinct values
Notes
- Uses HyperLogLog algorithm for efficient approximate counting
- Much faster than COUNT(DISTINCT) for large datasets
- Provides probabilistic estimate with controllable error rate
- NULL values are excluded from the count
- Precision parameter controls accuracy vs memory tradeoff
- Typical accuracy: within 2-3% of exact count
- Returns 0 for empty groups
- Can be used with WHERE clause to filter before aggregation
- Can be used with GROUP BY clause for grouped aggregation