# Angular Data Grid Aggregates

The aggregates in the [Angular Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid) help you to easily perform various mathematic operations with the values of grouped columns. Aggregates are commonly displayed inside [Group Templates](https://www.telerik.com/kendo-angular-ui-develop/components/grid/grouping/templates).

The following example demonstrates how to aggregate the data by the `Amount` field.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/grouping/aggregates/source.txt)

To apply aggregates by specific fields:

1. Create an array of [`AggregateDescriptor`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/aggregatedescriptor) objects that define aggregates for the required fields.

   ```ts
   public myAggregates: AggregateDescriptor[] = [
       { field: 'Amount', aggregate: 'sum' }
   ];
   ```

2. Set the created aggregates array to the `aggregates` property of the [`GroupDescriptor`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/groupdescriptor) for the respective field.

   ```ts
   public group: GroupDescriptor[] = [
       { field: 'transactionType', aggregates: this.myAggregates }
   ];
   ```

   By design, the aggregates are not persisted, and you must apply them manually. Make sure to update the `aggregates` property of the `GroupDescriptor` in the [`groupChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#groupchange) or [`dataStateChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#datastatechange) event handler.

   ```ts
   onGroupChange(group) {
       // Reapply aggregates because grouping changes do not preserve them.
       group.map((group) => (group.aggregates = this.aggregates));

       this.group = group;
       this.gridData = groupBy(sampleProducts, this.group);
   }
   ```

3. Use the `aggregates` field passed by each template to access and display the created aggregates from step 1.

   ```html
   <kendo-grid [group]="group"...>
       <kendo-grid-column field="amount">
           <ng-template kendoGridGroupFooterTemplate  let-aggregates="aggregates">
                   <span>
                       Sum: {{aggregates.amount.sum}}
                   </span>
           </ng-template>
       </kendo-grid-column>
   </kendo-grid>
   ```

## Aggregate Types

The Grid supports the following aggregate functions for mathematical calculations on grouped data:

* `sum`—Calculates the total of all numeric values in the group
* `average`—Calculates the average of all numeric values in the group
* `count`—Returns the number of items in the group
* `min`—Finds the smallest value in the group
* `max`—Finds the largest value in the group

The following example demonstrates how to calculate several aggregates and display them in the footer template.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/grouping/aggregates/types/source.txt)

## Suggested Links

* [Group Templates](https://www.telerik.com/kendo-angular-ui-develop/components/grid/grouping/manual-grouping)
* [Overview of Data Query](https://www.telerik.com/kendo-angular-ui-develop/components/data-query)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [API Index of the Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api)
