Docs
On this page
reference2 min readApplication: Platform

Aggregate

Methods of the Aggregate object, which counts, sums and groups table records in the database from server scripts.

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 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().

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.

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, 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

Servicely Script

Aggregate("User")
    .COUNT(AS("UserCount"))
    .COUNT("Email", AS("EmailCount"))
    .AND(
        EQUAL("Active", true),
        GT("FailedLoginAttempts", 0)
    )
    .fetchSingle();

Response

{UserCount=8, EmailCount=6}

Count and sum together

Servicely Script

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

result.Breached;

Response

// result is {Breached=4, TargetDurationSum=115320000}
4

Group the results

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

Servicely Script

Aggregate("Trigger")
    .IN("Table", ["Incident", "ITSMRequest"])
    .GROUP_BY("Table")
    .GROUP_BY("TriggerWhen", AS("When"))
    .COUNT("TriggerWhen", AS("Total"))
    .fetch();

Response

[
  {
    "TableGroup": "ITSMRequest",
    "TableGroupDisplayValue": "ITSM Request",
    "Total": 2,
    "When": "after",
    "WhenDisplayValue": "After"
  },
  {
    "TableGroup": "Incident",
    "Total": 6,
    "When": "before",
    "WhenDisplayValue": "Before"
  }
]

Filter groups with HAVING

Aggregate("Trigger")
    .GROUP_BY("TriggerWhen", AS("When"))
    .COUNT("TriggerWhen", AS("Total"))
    .HAVING(GT("Total", 60))   // only groups with more than 60 triggers
    .fetch();
Last updated 3 December 2026commit 6777698daac5Chris Jones
Back to top