> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# max_n_by()

> Track the largest values and associated data in a set of values

<Icon icon="tag" iconType="duotone" /> Since [1.16.0][toolkit-1.16.0]

Construct an aggregate that keeps track of the largest values passed through it, as well as some associated data which
is passed alongside the value.

## Arguments

The syntax is:

```sql theme={"dark"}
max_n_by(
    value BIGINT | DOUBLE PRECISION | TIMESTAMPTZ,
    data ANYELEMENT,
    capacity BIGINT
) MaxNBy
```

| Name       | Type                                      | Default | Required | Description                                 |
| ---------- | ----------------------------------------- | ------- | -------- | ------------------------------------------- |
| `value`    | BIGINT \| DOUBLE PRECISION \| TIMESTAMPTZ | -       | ✔        | The values passed into the aggregate        |
| `data`     | ANYELEMENT                                | -       | ✔        | The data associated with a particular value |
| `capacity` | BIGINT                                    | -       | ✔        | The number of values to retain.             |

## Returns

| Column     | Type   | Description                                                                                                                |
| ---------- | ------ | -------------------------------------------------------------------------------------------------------------------------- |
| max\_n\_by | MaxNBy | The compiled aggregate. Note that the exact type will be MaxByInts, MaxByFloats, or MaxByTimes depending on the input type |

[toolkit-1.16.0]: https://github.com/timescale/timescaledb-toolkit/releases/tag/1.16.0
