---
title: "Aggregate"
site: api
version: 1.11
versions: "all"
url: https://docs-new.servicely.ai/api/1.11/scripting/aggregate/
type: reference
application: platform
last_updated: 2026-10-19
commit: f1879fd7e955
---


`Aggregate` counts, sums and groups the records of a table in the database, without loading the records into the script. It is a global in every server script. Use it instead of reading records with [Table](page:scripting/table) when you only need totals.

## Get an instance

Call `Aggregate` with a table name, or call `getAggregate()` on a Table. Then add one or more aggregations and call `fetch()` or `fetchSingle()`.

```javascript
let users = Aggregate("User");
let sameUsers = Table("User").getAggregate();
```

`Aggregate` uses the `DefaultChecks` set, so permissions and restrictions do not limit the result. See [TableProtected and TableChecks](page:scripting/table-checks).

## Aggregation methods

Each aggregation adds a column to the result and returns the Aggregate, so you can chain them. Pass `AS("name")` as the last argument to name the column.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `COUNT()` | Aggregate | Counts the records. The column is named `Count`. | 1.11 |
| `COUNT(field)` | Aggregate | Counts the records where the field has a value. The column is named after the field, for example `EmailCount`. | 1.11 |
| `DISTINCT(field)` | Aggregate | Counts the distinct values of the field. | 1.11 |
| `SUM(field)` | Aggregate | Adds up the field's values. | 1.11 |
| `MIN(field)` | Aggregate | Returns the field's lowest value. | 1.11 |
| `MAX(field)` | Aggregate | Returns the field's highest value. | 1.11 |
| `AVG(field)` | Aggregate | Returns the average of the field's values. | 1.11 |
| `GROUP_BY(field)` | Aggregate | Returns one result row per value of the field. When the field type has a display value, the row also holds it, in a column ending `DisplayValue`. | 1.11 |
| `HAVING(criterion)` | Aggregate | Keeps only the rows that meet the criterion. The criterion's field is an aggregation's column name. | 1.11 |
| `AS(name)` | Alias | Names the column of the aggregation it is passed to. The name starts with a capital letter in the result. | 1.11 |

Without `AS`, a column is named after the field and the aggregation, for example `FailedLoginAttemptsSum` or `CompanyGroup`.

## Query and result methods

The [query operators](page:scripting/query-operators), such as `EQUAL` and `IN`, chain on an Aggregate as they do on a Table.

| Method | Returns | Description | Since |
| --- | --- | --- | --- |
| `fetch()` | Array | Runs the aggregate and returns one object per result row. | 1.11 |
| `fetchSingle()` | Object | Runs the aggregate and returns the first result row. | 1.11 |
| `forEachPage(callback)` | Nothing | Runs the aggregate a page at a time and calls the function with each page of rows and the page number. | 1.11 |
| `forEachPaginated(callback)` | Nothing | Runs the aggregate a page at a time and calls the function with each row and its index. | 1.11 |
| `addCriteria(criterion)` | Aggregate | Adds a criterion. | 1.11 |
| `addJsonQuery(jsonQuery)` | Aggregate | Adds a query in JSON form, as a string or an object. | 1.11 |
| `maxResults(count)` | Aggregate | Sets the maximum number of result rows. Without it, an aggregate returns at most 10,000 rows. | 1.11 |
| `sql()` | String | Returns the SQL the aggregate generates. `sql(true)` writes the values in. | 1.11 |

`fetch()` and `fetchSingle()` throw an error if no aggregation was added.

## Examples

### Count with a filter and a name

<CodeTabs>
  Servicely Script

  ```javascript
  Aggregate("User")
      .COUNT(AS("UserCount"))
      .COUNT("Email", AS("EmailCount"))
      .AND(
          EQUAL("Active", true),
          GT("FailedLoginAttempts", 0)
      )
      .fetchSingle();
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  {UserCount=8, EmailCount=6}
  ```
</CodeTabs>

### Count and sum together

<CodeTabs>
  Servicely Script

  ```javascript
  let result = Aggregate("SLA")
      .COUNT(AS("Breached"))
      .SUM("TargetDuration")
      .EQUAL("ProgressLevel", "Breached")
      .fetchSingle();

  result.Breached;
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  // result is {Breached=4, TargetDurationSum=115320000}
  4
  ```
</CodeTabs>

### Group the results

This counts the triggers of two tables, by table and by when they run.

<CodeTabs>
  Servicely Script

  ```javascript
  Aggregate("Trigger")
      .IN("Table", ["Incident", "ITSMRequest"])
      .GROUP_BY("Table")
      .GROUP_BY("TriggerWhen", AS("When"))
      .COUNT("TriggerWhen", AS("Total"))
      .fetch();
  ```
</CodeTabs>

<CodeTabs>
  Response

  ```javascript
  [
    {
      "TableGroup": "ITSMRequest",
      "TableGroupDisplayValue": "ITSM Request",
      "Total": 2,
      "When": "after",
      "WhenDisplayValue": "After"
    },
    {
      "TableGroup": "Incident",
      "Total": 6,
      "When": "before",
      "WhenDisplayValue": "Before"
    }
  ]
  ```
</CodeTabs>

### Filter groups with HAVING

```javascript
Aggregate("Trigger")
    .GROUP_BY("TriggerWhen", AS("When"))
    .COUNT("TriggerWhen", AS("Total"))
    .HAVING(GT("Total", 60))   // only groups with more than 60 triggers
    .fetch();
```

## Related

- [Table](page:scripting/table)
- [Query operators and criteria](page:scripting/query-operators)
- [Querying tables](page:scripting/querying-tables)
- [TableProtected and TableChecks](page:scripting/table-checks)
