Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Fcommands-reference%2Fcountby.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Fcommands-reference%2Fcountby.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `countby`

## Description

The `countby` command generates a count for each distinct value in a given expression, effectively grouping the results by that key.

Note

Unlike `count`, which tallies all records in a set, `countby` provides a per-group count based on the specified key or expression.

## Syntax

```
countby <expression> [as <alias>] [into <keypath>] [(asc|desc)]
```

## Example

**Use case: Count requests by HTTP status code**

When analyzing logs, it's often useful to understand the distribution of response types. The `countby` command groups documents by `status_code` and counts the number of requests in each group.

### Example data

```
{ "status_code": 200, "path": "/home" },

{ "status_code": 200, "path": "/about" },

{ "status_code": 404, "path": "/missing" },

{ "status_code": 500, "path": "/checkout" }
```

### Example query

```
countby status_code into request_count asc
```

### Example output

| status\_code | request\_count |
| ------------ | -------------- |
| 404          | 1              |
| 500          | 1              |
| 200          | 2              |

The `countby` command produces one record per unique status code, with a count of how many times each appears. Using `asc` orders the results from the smallest count to the largest.
