GlideAggregate - Global
-
- UpdatedAug 1, 2024
- 21 minutes to read
- Xanadu
- API reference
The GlideAggregate API enables creating database aggregation queries.
The GlideAggregate class is an extension of the GlideRecord class and provides database aggregation (AVG, COUNT, GROUP_CONCAT, GROUP_CONCAT_DISTINCT, MAX, MIN, STDDEV, SUM) queries. This functionality can be helpful when creating customized reports or in calculations for calculated fields.
When you use GlideAggregate methods on currency or price fields, you are working with the reference currency value. Be sure to convert the aggregate values to the user's session currency for display. Because the conversion rate between the currency or price value (displayed value) and its reference currency value (aggregation value) might change, the result may not be what the user expects.
To use this API to create dynamic attributes you must have the dynamic_schema_writer role. To read dynamic data using this API you must have the dynamic_schema_reader role.
For more information on dynamic attributes, see Dynamic Schema.
GlideAggregate - addAggregate(String agg, String name)
Adds an aggregate to a database query.
Name | Type | Description |
---|---|---|
agg | String | Name of an aggregate to use. Valid values:
|
name | String | Optional for field name. Name of the field, or path to an attribute within a dynamic attribute store, to group the results of the aggregation by. Format of the dynamic path: dyn_att_field->group_name->attribute_name
For more information on dynamic attributes, see Dynamic Schema. Default: Null |
Type | Description |
---|---|
None |
Example
The following example shows how to use GlideAggregate functions on the Incident [incident] table.
Output.
Example
The following example shows how to call this method using an attribute in a dynamic attribute store.
Output:
Scoped equivalent
To use the addAggregate() method in a scoped application, use the corresponding scoped method: addAggregate().
GlideAggregate - addBizCalendarTrend(String fieldName, String bizCalendarSysId)
Adds trending by a business calendar to the aggregate query. This method allows you to pick a date and time field in the corresponding GlideRecord and group records based on a specified business calendar time span.
Name | Type | Description |
---|---|---|
fieldName | String | Date and time field in the associated GlideRecord to use to determine in which group or calendar time span that the record will be included. |
bizCalendarSysId | String | Sys_id of the calendar record to use. This is the calendar that contains the desired time spans. |
Type | Description |
---|---|
None |
Example
The following code example shows the count of incident records grouped by the “Month” business calendar time spans.
Output:
GlideAggregate - addEncodedQuery(String query)
Adds an encoded query to the other queries that may have been set for this aggregate.
Name | Type | Description |
---|---|---|
query | String | Encoded query string to add to the aggregate. |
Type | Description |
---|---|
None |
Example
Scoped equivalent
To use the addEncodedQuery() method in a scoped application, use the corresponding scoped method: addEncodedQuery().
GlideAggregate - addHaving(String name, String operator, String value)
Adds a "having" element to the aggregate, such as select category, count(*) from incident group by category HAVING count(*) > 5.
Name | Type | Description |
---|---|---|
name | String | Aggregate to filter on. For example, COUNT. |
operator | String | Operator symbol. For example <, >, =, !=. |
value | String | Value to query on. For example, '5'. |
Type | Description |
---|---|
None |
Example
Output:
GlideAggregate - addHaving(String aggName, String fieldName, String operator, String value)
Adds a "having" element to the aggregate, such as select category, count(*) from incident group by category HAVING count(*) > 5. This implementation of the method enables you to specify a specific field within a table or a dynamic attribute to act upon.
Type | Description |
---|---|
None |
Example
The following code example shows how to identify the duplicate serial numbers within the cmdb table using a field name in the addHaving() method call.
Output:
Example
The following example shows to identify duplicates, but uses a dynamic attribute instead of a field in the addHaving() method call.
Output:
GlideAggregate - addTrend(String fieldName, String timeInterval, Number numUnits)
Adds a trend for a field. Use a trend to show patterns over a period of time.
Name | Type | Description |
---|---|---|
fieldName | String | Name of the field for which trending should occur. |
timeInterval | String | Time interval for the trend. Valid values:
|
numUnits | Number | Optional. Only valid when timeInterval =
minute. Number of minutes to include in the trend. Default: 1 |
Type | Description |
---|---|
None |
Example
Output:
Scoped equivalent
To use the addTrend() method in a scoped application, use the corresponding scoped method: addTrend().
GlideAggregate - getAggregate(String agg, String name)
Gets the value of an aggregate from the current record.
Name | Type | Description |
---|---|---|
agg | String | Type of the aggregate. Valid values:
|
name | String | Name of the field, or path to an attribute within a dynamic schema, to get the aggregate from. Format of the dynamic path: dyn_att_field->group_name->attribute_name
For more information on dynamic attributes, see Dynamic Schema. |
Type | Description |
---|---|
String | Value of the aggregation. If the values being aggregated are FX Currency
values, the returned value is in the format
<currency_code;currency_value> , such as:
USD;134.980000. Note: If the specified field
contains FX Currency values of mixed currency types, the method is not able to
aggregate the values and returns a semicolon (;). |
Example
This example shows how to obtain the COUNT aggregate.
Example
This example shows the aggregation of an FX Currency field.
Output:
Example
The following example shows how to call this method using an attribute in a dynamic attribute store.
Output:
Scoped equivalent
To use the getAggregate() method in a scoped application, use the corresponding scoped method: getAggregate().
GlideAggregate - getDynamicAttributeValue(String fullPath)
Returns the value of the dynamic attribute located at a specified path.
Name | Type | Description |
---|---|---|
fullPath | String | Path to use to locate the desired dynamic attribute. Format of the dynamic path: dyn_att_field->group_name->attribute_name
|
Type | Description |
---|---|
Object | Value of the dynamic attribute located at the specified path. If the fullPath parameter contains invalid data or the specified attribute value isn't one of the supported data types, returns null. |
Example
The following code example shows how to call this method.
GlideAggregate - getDynamicAttributeValue(String dynamicAttributeField, String groupAttrPath)
Returns the value of the dynamic attribute located at a specified field in the current table and a specified attribute path.
Name | Type | Description |
---|---|---|
dynamicAttributeField | String | Name of the field in the table that contains the dynamic attribute. |
groupAttrPath | String | Attribute path to use to locate the associated dynamic schema attribute. Format: "group_name->attr_name"
For example: |
Type | Description |
---|---|
Object | Value of the dynamic attribute located at the specified path. If either the dynamicAttributeField or groupAttrPath parameters are invalid, returns null. |
Example
The following code example shows how to call this method.
GlideAggregate - getDynamicAttributeDisplayValue(String fullPath)
Returns the display value of the dynamic attribute located at the specified path.
Name | Type | Description |
---|---|---|
fullPath | String | Path to use to locate the desired dynamic attribute. Format of the dynamic path: dyn_att_field->group_name->attribute_name
|
Type | Description |
---|---|
String | Display value of the dynamic attribute located at the specified path. If the fullPath parameter is invalid, returns null. |
Example
The following code example shows how to call this method.
GlideAggregate - getDynamicAttributeDisplayValue(String dynamicAttributeField, String groupAttrPath)
Returns the display value of the dynamic attribute located in a specified table field and attribute path.
Name | Type | Description |
---|---|---|
dynamicAttributeField | String | Name of the field in the table that contains the dynamic attribute. |
groupAttrPath | String | Attribute path to use to locate the associated dynamic schema attribute. Format: "group_name->attr_name"
For example: |
Type | Description |
---|---|
String | Display value of the dynamic attribute located at the specified path. If either the dynamicAttributeField or groupAttrPath parameters are invalid, returns null. |
Example
The following code example shows how to call this method.
GlideAggregate - getQuery()
Retrieves the query necessary to return the current aggregate.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
String | The query. |
Example
GlideAggregate - getRowCount()
Retrieves the number of rows in the GlideAggregate object.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
Number | Number of rows in the GlideAggregate object. |
Example
Scoped equivalent
To use the getRowCount() method in a scoped application, use the corresponding scoped method: Scoped GlideAggregate - getRowCount().
GlideAggregate - getTotal(String agg, String name)
Returns the number of records by summing an aggregate.
Name | Type | Description |
---|---|---|
agg | String | Name of an aggregate to use. Valid values:
|
name | String | Name of the field to aggregate. |
Type | Description |
---|---|
Number | Number of records. |
Example
Output:
GlideAggregate - getValue(String name)
Returns the value of a field or a dynamic attribute.
Name | Type | Description |
---|---|---|
name | String | Field name or path to an attribute within a dynamic attribute store. Format of the dynamic path: dyn_att_field->group_name->attribute_name
|
Type | Description |
---|---|
String | Value of the specified field. Returns null if invalid (not part of result set). |
Example
Output:
Scoped equivalent
To use the getValue() method in a scoped application, use the corresponding scoped method: getValue().
GlideAggregate - groupBy(String name)
Provides the name of a field, or an attribute within a dynamic attribute store, to use when grouping the aggregates.
May be called numerous times to set multiple group fields.
Name | Type | Description |
---|---|---|
name | String | Field name or path to an attribute within a dynamic attribute store. Format of the dynamic path: dyn_att_field->group_name->attribute_name
For more information on dynamic attributes, see Dynamic Schema. |
Type | Description |
---|---|
None |
Example
The following example shows how to call this method.
Example
The following example shows how to call this method using an attribute in a dynamic attribute store.
Scoped equivalent
To use the groupBy() method in a scoped application, use the corresponding scoped method: groupBy().
GlideAggregate - orderBy(String name)
Orders the aggregates using the value of the specified field, dynamic attribute path, or glidefunction. The field is also added to the group-by list.
Name | Type | Description |
---|---|---|
name | String | Field name, path to an attribute within a dynamic attribute store, or glidefunction to use to order the aggregates. Format of the dynamic path: dyn_att_field->group_name->attribute_name
For more information on dynamic attributes, see Dynamic Schema. glidefunction format: |
Type | Description |
---|---|
None |
Example
Scoped equivalent
To use the orderBy() method in a scoped application, use the corresponding scoped method: orderBy().
GlideAggregate - orderByAggregate(String agg, String fieldName)
Orders the aggregates based on the specified aggregate and field or dynamic attribute.
Name | Type | Description |
---|---|---|
agg | String | Type of aggregation. Valid values:
|
fieldName | String | Name of the field or path to an attribute within a dynamic attribute store to aggregate. Format of the dynamic path: dyn_att_field->group_name->attribute_name
For more information on dynamic attributes, see Dynamic Schema. |
Type | Description |
---|---|
None |
Example
Scoped equivalent
To use the orderByAggregate() method in a scoped application, use the corresponding scoped method: orderByAggregate().
GlideAggregate - query()
Issues the query and gets the results.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
None |
Example
Scoped equivalent
To use the query() method in a scoped application, use the corresponding scoped method: query().
GlideAggregate - setAggregateWindow(Number firstRow, Number lastRow)
Limits the number of rows from the table to include in the aggregate query.
Name | Type | Description |
---|---|---|
firstRow | Number | Zero-based index of the first row to include in the aggregate query, inclusive. |
lastRow | Number | Zero-based index of the last row to include in the aggregate query, exclusive. |
Type | Description |
---|---|
None |
Example
Prints the count of each category for the first ten records in the Incident [incident] table.
var incidentGroup = new GlideAggregate('incident');
incidentGroup.addAggregate('COUNT', 'category');
incidentGroup.setAggregateWindow(0, 10);
incidentGroup.query();
while (incidentGroup.next()) {
var incidentCount = incidentGroup.getAggregate('COUNT', 'category');
gs.info('{0} count: {1}', [incidentGroup.getValue('category'), incidentCount]);
}
Output:
database count: 1
Hardware count: 1
inquiry count: 7
software count: 1
Scoped equivalent
To use the setAggregateWindow() method in a scoped application, use the corresponding scoped method: setAggregateWindow().
GlideAggregate - setGroup(Boolean b)
Sets whether to group the results.
Name | Type | Description |
---|---|---|
b | Boolean | Flag that indicates whether to group the results. Valid values:
|
Type | Description |
---|---|
None |
Example
Scoped equivalent
To use the setGroup() method in a scoped application, use the corresponding scoped method: setGroup().
GlideAggregate - setIntervalYearIncluded(Boolean b)
Sets whether to group results by year for day-of-week trends. These trends are created using the addTrend() method with the dayofweek time interval.
Dependency: GlideAggregate - addTrend('<fieldName>', 'dayofweek').
Name | Type | Description |
---|---|---|
b | Boolean | Flag that indicates whether to include a year for a trend with a day-of-week time interval. Valid values:
Default: true |
Type | Description |
---|---|
None |
Example
The following example shows how to count incidents created in the last six months. The incidents are separated by the day of the week, but not including the year. For example, the default results for Thursday would include the
year, such as Thursday/2023: 16
.
Output:
Scoped equivalent
To use the setIntervalYearIncluded() method in a scoped application, use the corresponding scoped method: setIntervalYearIncluded().
On this page
- GlideAggregate - addAggregate(String agg, String name)
- GlideAggregate - addBizCalendarTrend(String fieldName, String bizCalendarSysId)
- GlideAggregate - addEncodedQuery(String query)
- GlideAggregate - addHaving(String name, String operator, String value)
- GlideAggregate - addHaving(String aggName, String fieldName, String operator, String value)
- GlideAggregate - addTrend(String fieldName, String timeInterval, Number numUnits)
- GlideAggregate - getAggregate(String agg, String name)
- GlideAggregate - getDynamicAttributeValue(String fullPath)
- GlideAggregate - getDynamicAttributeValue(String dynamicAttributeField, String groupAttrPath)
- GlideAggregate - getDynamicAttributeDisplayValue(String fullPath)
- GlideAggregate - getDynamicAttributeDisplayValue(String dynamicAttributeField, String groupAttrPath)
- GlideAggregate - getQuery()
- GlideAggregate - getRowCount()
- GlideAggregate - getTotal(String agg, String name)
- GlideAggregate - getValue(String name)
- GlideAggregate - groupBy(String name)
- GlideAggregate - orderBy(String name)
- GlideAggregate - orderByAggregate(String agg, String fieldName)
- GlideAggregate - query()
- GlideAggregate - setAggregateWindow(Number firstRow, Number lastRow)
- GlideAggregate - setGroup(Boolean b)
- GlideAggregate - setIntervalYearIncluded(Boolean b)