---
title: 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.
url: https://coralogix.com/docs/dataprime/language-reference/commands-reference/countby/
product: dataengine
feature: DataPrime Language
feature_id: dataprime-language
feature_identifier: dataengine/dataprime-language
last_updated: '2026-10-21'
---

# `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.
