> For the complete documentation index, see [llms.txt](https://docs.scanner.dev/scanner/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.scanner.dev/scanner/using-scanner-complete-feature-reference/querying-and-analysis/aggregation-functions/groupbycount.md).

# groupbycount()

`groupbycount(col [, ...cols])` counts the number of occurrences of each distinct value of the provided column(s). If multiple columns are provided, their values are treated as a tuple.

Note that `groupbycount col` is effectively the same as `stats by col`, differing only in its approximation behavior for very large numbers of groups.

### Technical Notes

* If there are fewer than 1000 distinct groups, `groupbycount` will return those groups and their exact counts.
* If there are >1000 distinct groups, it will return the 1000 most numerous groups, and will approximate their counts using a variant of the [count-min sketch](https://en.wikipedia.org/wiki/Count%E2%80%93min_sketch)—specifically the count-mean-min sketch detailed [here](https://webdocs.cs.ualberta.ca/~drafiei/papers/cmm.pdf).
  * For most inputs, this has a p99 error of 0.1%.
* `groupbycount` does not currently respect `max_bytes` in the API, and will always return at most 1000 distinct groups and their counts.

## Returns

A table with one row for each distinct value of `col [, ...cols]`, with the following columns:

* One column for each `col` provided, containing the value of `col`.
* `@q.count`, containing the number of occurrences of that value.

## Examples

Analyze AWS CloudTrail logs to find the most frequently called AWS APIs.

```python
@scnr.source_type: "aws:cloudtrail"
| groupbycount(eventSource)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.scanner.dev/scanner/using-scanner-complete-feature-reference/querying-and-analysis/aggregation-functions/groupbycount.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
