Skip to content

Commit

Permalink
Add doc for APPROX_COUNT_DISTINCT aggregate function (#20188) (#20245)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Feb 12, 2025
1 parent 779c395 commit 42da033
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion functions-and-operators/aggregate-group-by-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,33 @@ In addition, TiDB also provides the following aggregate functions:
1 row in set (0.00 sec)
```

Except for the `GROUP_CONCAT()` and `APPROX_PERCENTILE()` functions, all the preceding functions can serve as [Window functions](/functions-and-operators/window-functions.md).
+ `APPROX_COUNT_DISTINCT(expr, [expr...])`

This function is similar to `COUNT(DISTINCT)` in counting the number of distinct values but returns an approximate result. It uses the `BJKST` algorithm, significantly reducing memory consumption when processing large datasets with a power-law distribution. Moreover, for low-cardinality data, this function provides high accuracy while maintaining efficient CPU utilization.

The following example shows how to use this function:

```sql
DROP TABLE IF EXISTS t;
CREATE TABLE t(a INT, b INT, c INT);
INSERT INTO t VALUES(1, 1, 1), (2, 1, 1), (2, 2, 1), (3, 1, 1), (5, 1, 2), (5, 1, 2), (6, 1, 2), (7, 1, 2);
```

```sql
SELECT APPROX_COUNT_DISTINCT(a, b) FROM t GROUP BY c;
```

```
+-----------------------------+
| approx_count_distinct(a, b) |
+-----------------------------+
| 3 |
| 4 |
+-----------------------------+
2 rows in set (0.00 sec)
```

Except for the `GROUP_CONCAT()`, `APPROX_PERCENTILE()`, and `APPROX_COUNT_DISTINCT` functions, all the preceding functions can serve as [Window functions](/functions-and-operators/window-functions.md).

## GROUP BY modifiers

Expand Down

0 comments on commit 42da033

Please sign in to comment.