# Aggregation

## Overview

An aggregation is a data provider which represents a database column that is aggregated over a set of records.

At runtime, aggregations are computed in the context of a foundset. The value is derived from a SQL query, which takes the form of a SQL Aggregate Function and appends the WHERE clause used by the foundset's query.

## Properties Summary

At design-time aggregations have the following properties:

| Property               | Summary                                              |
| ---------------------- | ---------------------------------------------------- |
| [Name](#name)          | The name of the aggregation                          |
| [Type](#returned-type) | The aggregating function to be applied to the column |
| [Column](#code)        | The column containing values that are aggregated     |

## Properties Details

### Name

The name by which the aggregation will be available as a data provider throughout the solution in which it is declared, as well as any modules containing it.

### Type

There are five types of aggregations:

* [**count**](/reference/servoycore/dev-api/database-manager/qbaggregate.md#count) - The number of records in an entire foundset containing a non-null value for a column
* [**count(distinct)**](/reference/servoycore/dev-api/database-manager/qbaggregate.md#distinct) - The number of distinct records in an entire foundset containing a non-null value for a column
* [**maximum**](/reference/servoycore/dev-api/database-manager/qbaggregate.md#max) - The largest value for a numeric column in an entire foundset
* [**minimum**](/reference/servoycore/dev-api/database-manager/qbaggregate.md#min) - The smallest value for a numeric column in an entire foundset
* [**average**](/reference/servoycore/dev-api/database-manager/qbaggregate.md#avg) - The average value for a numeric column in an entire foundset
* [**sum**](/reference/servoycore/dev-api/database-manager/qbaggregate.md#sum) - The sum of all the values for numeric column in an entire foundset

### Column

The column containing values that are aggregated.

### Example

Assume an aggregation, record\_count, declared on the customer table. The aggregation type is count and the column is customerid. The aggregation is available for any foundset based on the customers table.

```javascript
function printRecordCcount(event) {
    application.output(record_count);       // print record count before find
    if(foundset.find()){                // Find all customers where city starts with 'B'
        city = 'B%';
        foundset.search();
        application.output(record_count);   // print the record count after find
    }
}
```

After the find, the aggregation is re-queried using the foundset's new WHERE clause.

```sql
SELECT COUNT(customerid) AS record_count FROM customers WHERE city LIKE ? LIMIT ?
```

{% hint style="warning" %}
**Performance**

Because aggregations are derived from SQL queries, they may not reflect data changes, seen in the client, but not yet committed to the database. Aggregations will refresh after outstanding changes are committed.

SQL Aggregate Functions may be expensive operations, depending on the size and structure of a database table and the nature of the aggregation. Developers are encouraged to use discretion when working with aggregations. For example, when an aggregation is shown in a table-view form, it may result in a query for each record in displayed on the form, and performance may degrade.
{% endhint %}


---

# 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.servoy.com/reference/servoycore/object-model/database-server/table/aggregation.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.
