> For the complete documentation index, see [llms.txt](https://learn.getvectorai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.getvectorai.com/vector-search/aggregation/writing-your-first-aggregation.md).

# Writing Your First Aggregation

Aggregations refers to when you group a collection by a certain field and then take a metric when it is grouped.&#x20;

An example of an aggregation query can be found as below:

```
{
    "groupby" : [
        {"name": <alias>, 
        "field": <field in the collection>, "agg": "category"},
        {"name": <alias>, 
        "field": <another groupby field in the collection>, 
        "agg": "numeric"}
    ],
    "metrics" : [
        {"name": <alias>, 
        "field": <numeric field in the collection>, 
        "agg": "avg"}
        {
        "name": <alias>, 
        "field": <another numeric field in the collection>, 
        "agg": "max"}
    ]
}

```

The available groupby types are:

* category (groupby a field that is a category)
* numeric (groupby a field that is numeric)

Metrics are the fields you want to calculate in each of these every aggregation includes a frequency metric. These are the available metric types:&#x20;

* average
* max
* min
* sum
* cardinality

To help memorise the aggregation queries, the following example can help:&#x20;

```
{
    "groupby" : [
        {"name": "region", "field": "player_region", "agg": "category"},
        {"name": "player_name", "field": "name", "agg": "category"}
    ],
    "metrics" : [
        {"name": "average_score", "field": "final_score", "agg": "avg"},
        {"name": "max_score", "field": "final_score", "agg": "max"},
        {'name':'total_score','field':"final_score", 'agg':'sum'},
        {'name':'average_deaths','field':"final_deaths", 'agg':'avg'},
        {'name':'highest_deaths','field':"final_deaths", 'agg':'max'},
    ]
}
```

In this example, we aggregate by 2 separate categories (region and player name). Based on each category, the average, max, sum of scores is calculated and the average and maximum number of deaths are calculated.&#x20;


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://learn.getvectorai.com/vector-search/aggregation/writing-your-first-aggregation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
