# Aggregate queries

Aggregate queries in GraphQL are useful for summarizing data in various ways. They help in performing operations such as counting the number of records, summing up numerical fields, and grouping data based on specific fields.

Add `_aggregate` with any asset type to perform aggregate queries. `_count`, `_sum` and `_groupby` are currently supported.

#### <mark style="color:blue;">Count Query</mark>

The `count` query allows you to count the number of records that match a specific condition.

```graphql
{
    Invoice_aggregate {
        _count
    }
}
```

Example:

```graphql
{
    Invoice_aggregate (status: "updated") {
        _count
    }
}
```

In this example, the query counts the invoices a 'updated' status.

***

#### <mark style="color:blue;">Sum Query</mark>

The `sum` query allows you to sum the values of a specific numerical field across all records that match a given condition.

```graphql
{
    Invoice_aggregate {
        _sum {
            amount
        }
    }
}
```

Example:

```graphql
query {
  Invoice_aggregate(where: { status: "updated" }) {
          _sum {
              amount
        }
      }
}
```

In this example, the query sums up the amount of all invoices that have a 'updated' status.

***

#### <mark style="color:blue;">Group By Query</mark>

The `groupBy` query allows you to group records by a specific field and perform aggregate functions like count or sum within each group.

```graphql
{
    Invoice_aggregate {
        _count
        _groupby{
            invoiceType
        }
    }
}
```

Example:

```graphql
{
    Invoice_aggregate (status: "updated") {
        _count
        _groupby{
            invoiceType
        }
    }
}
```

This query groups invoices by their type and provides the count of the invoices with 'updated' status.


---

# Agent Instructions: 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:

```
GET https://docs.spydra.app/developers/api-reference/asset-tokenization/graphql/aggregate-queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
