APPROX_COUNT_DISTINCT

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Computes an approximation of the count of distinct values with a specified expected relative error.

Syntax 

1approx_count_distinct(<expression> [, <error>])

Arguments 

Required 

  • <expression>: An expression of any type to count distinct values for.

Optional 

  • <error>: A double precision value specifying the expected relative error. Must be in the range (0.002, 1]. Defaults to 0.023 (2.3% expected relative error).

Returns 

Returns a bigint representing the approximate count of distinct values.

Considerations 

  • Faster than exact count(distinct expression) for large datasets.
  • The error parameter controls the trade-off between accuracy and performance.
  • Lower error values provide more accurate results but may require more computation.

Examples 

Approximate Distinct Count 

Get an approximate count of distinct values.

1SELECT approx_count_distinct(customer_id) AS approx_customers
2FROM orders;

With Custom Error Rate 

Specify a custom error rate for higher accuracy.

1SELECT approx_count_distinct(product_id, 0.01) AS approx_products
2FROM sales;

Returns an approximate count with 1% expected relative error.

Related Documentation