Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc for APPROX_COUNT_DISTINCT aggregate function #20188

Merged
merged 6 commits into from
Feb 12, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions functions-and-operators/aggregate-group-by-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@

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...])`

The usage of this function is almost same with `COUNT(DISTINCT)` but returns approximate result. It uses `BJKST` algorithm and consumes less memory during simultaneous computation of cardinality for a large number of data sets whose cardinality has power law distribution. Moreover, it's very accurate for data sets with small cardinality and very efficient on CPU.

Check warning on line 67 in functions-and-operators/aggregate-group-by-functions.md

View workflow job for this annotation

GitHub Actions / vale

[vale] reported by reviewdog 🐶 [PingCAP.Ambiguous] Consider using a clearer word than 'a large number of' because it may cause confusion. Raw Output: {"message": "[PingCAP.Ambiguous] Consider using a clearer word than 'a large number of' because it may cause confusion.", "location": {"path": "functions-and-operators/aggregate-group-by-functions.md", "range": {"start": {"line": 67, "column": 204}}}, "severity": "INFO"}
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved

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;
```

```sql
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved
xzhangxian1008 marked this conversation as resolved.
Show resolved Hide resolved
+-----------------------------+
| approx_count_distinct(a, b) |
+-----------------------------+
| 3 |
| 4 |
+-----------------------------+
2 rows in set (0.00 sec)
```

## GROUP BY modifiers

Starting from v7.4.0, the `GROUP BY` clause of TiDB supports the `WITH ROLLUP` modifier. For more information, see [GROUP BY modifiers](/functions-and-operators/group-by-modifier.md).
Expand Down
Loading