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.

Note: When using an on-premise system, the database server time zone must be set to GMT/UTC for this class to work properly.

Scoped GlideAggregate - GlideAggregate(String tableName)

Creates a GlideAggregate object on the specified table.

Table 1. Parameters
Name Type Description
tableName String Name of the table.

Example

var count = new GlideAggregate('incident');

Scoped GlideAggregate - addAggregate(String agg, String name)

Adds an aggregate to a database query.

Table 2. Parameters
Name Type Description
agg String Name of an aggregate to use.
Valid values:
  • AVG: Average value of the expression.
  • COUNT: Count of the number of non-null values.
  • GROUP_CONCAT: Concatenates all non-null values of the group in ascending order, joins them with a comma (','), and returns the result as a string.
  • GROUP_CONCAT_DISTINCT: Concatenates all non-null values of the group in ascending order, removes duplicates, joins them with a comma (','), and returns the result as a string.
  • MAX: Largest, or maximum, value.
  • MIN: Minimum value.
  • STDDEV: Population standard deviation.
  • SUM: Sum of all values.
name String Optional. Name of the field to group the results of the aggregation by.

Default: Null

Table 3. Returns
Type Description
None

Example

The following example shows how to use GlideAggregate functions on the Incident [incident] table.

var incidentGA = new GlideAggregate('incident');
incidentGA.groupBy('category');
incidentGA.orderByAggregate('COUNT', 'sys_mod_count');

incidentGA.addAggregate('AVG', 'sys_mod_count');
incidentGA.addAggregate('COUNT', 'sys_mod_count');
incidentGA.addAggregate('GROUP_CONCAT', 'sys_mod_count');
incidentGA.addAggregate('GROUP_CONCAT_DISTINCT', 'sys_mod_count');
incidentGA.addAggregate('MAX', 'sys_mod_count');
incidentGA.addAggregate('MIN', 'sys_mod_count');
incidentGA.addAggregate('STDDEV', 'sys_mod_count');
incidentGA.addAggregate('SUM', 'sys_mod_count');

incidentGA.query();

while (incidentGA.next()) {
gs.info('CATEGORY: ' + incidentGA.getValue('category'));
gs.info('AVG: ' + incidentGA.getAggregate('AVG', 'sys_mod_count'));
gs.info('COUNT: ' + incidentGA.getAggregate('COUNT', 'sys_mod_count'));
gs.info('GROUP_CONCAT: ' + incidentGA.getAggregate('GROUP_CONCAT', 'sys_mod_count'));
gs.info('GROUP_CONCAT_DISTINCT: ' + incidentGA.getAggregate('GROUP_CONCAT_DISTINCT', 'sys_mod_count'));
gs.info('MAX: ' + incidentGA.getAggregate('MAX', 'sys_mod_count'));
gs.info('MIN: ' + incidentGA.getAggregate('MIN', 'sys_mod_count'));
gs.info('STDDEV: ' + incidentGA.getAggregate('STDDEV', 'sys_mod_count'));
gs.info('SUM: ' + incidentGA.getAggregate('SUM', 'sys_mod_count'));
gs.info(' ');
}

Output.

CATEGORY: inquiry
AVG: 8.42424242424242424242424242424242424242E00
COUNT: 33
GROUP_CONCAT: 0,0,0,0,1,2,2,4,4,4,5,5,5,6,6,6,6,6,6,6,6,7,8,8,8,8,9,15,15,19,32,33,36
GROUP_CONCAT_DISTINCT: 0,1,2,4,5,6,7,8,9,15,19,32,33,36
MAX: 36
MIN: 0
STDDEV: 9.15843294125113629932710147135171494439E00
SUM: 278
  
CATEGORY: software
AVG: 21
COUNT: 13
GROUP_CONCAT: 3,4,5,5,6,7,9,10,10,11,14,94,95
GROUP_CONCAT_DISTINCT: 3,4,5,6,7,9,10,11,14,94,95
MAX: 95
MIN: 3
STDDEV: 3.27693962918655755532970326989852794087E01
SUM: 273
  
CATEGORY: Hardware
AVG: 6.8
COUNT: 10
GROUP_CONCAT: 2,5,5,6,6,6,7,7,9,15
GROUP_CONCAT_DISTINCT: 2,5,6,7,9,15
MAX: 15
MIN: 2
STDDEV: 3.39280283999985933622820108982884699755E00
SUM: 68
  
CATEGORY: network
AVG: 18
COUNT: 5
GROUP_CONCAT: 3,12,17,21,37
GROUP_CONCAT_DISTINCT: 3,12,17,21,37
MAX: 37
MIN: 3
STDDEV: 1.25698050899765347157025586536136512302E01
SUM: 90
  
CATEGORY: 
AVG: 9.5
COUNT: 4
GROUP_CONCAT: 8,8,10,12
GROUP_CONCAT_DISTINCT: 8,10,12
MAX: 12
MIN: 8
STDDEV: 1.91485421551267621995020382273964310607E00
SUM: 38
  
CATEGORY: database
AVG: 29
COUNT: 2
GROUP_CONCAT: 8,50
GROUP_CONCAT_DISTINCT: 8,50
MAX: 50
MIN: 8
STDDEV: 2.969848480983499602483546320840365965E01
SUM: 58

Scoped GlideAggregate - addEncodedQuery(String query)

Adds an encoded query to the other queries that may have been set for this aggregate.

Table 4. Parameters
Name Type Description
query String An encoded query to add to the aggregate.
Table 5. Returns
Type Description
None

Example

//Number of incidents varies depending on the current state
//of the incident table
var count = new GlideAggregate('incident');
count.addEncodedQuery('active=true');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next())
   incidents = count.getAggregate('COUNT');
gs.info(incidents);

Scoped GlideAggregate - addQuery(String name, String operator, String value)

Adds a query to the aggregate.

Table 6. Parameters
Name Type Description
name String Query to add.
operator String Operator for the query.
value String List of values to include in the query.
Table 7. Returns
Type Description
GlideQueryCondition Query condition.

Example

//Number of incidents varies depending on the current state
//of the incident table
var count = new GlideAggregate('incident');
count.addQuery('active', '=','true');
count.addAggregate('COUNT', 'category');
count.query();  
while (count.next()) {
   var category = count.category;
   var categoryCount = count.getAggregate('COUNT', 'category');
   gs.info("There are currently " + categoryCount + " incidents with a category of " + category);
}
Output:
There are currently 1 incidents with a category of database
There are currently 5 incidents with a category of hardware
There are currently 42 incidents with a category of inquiry
There are currently 4 incidents with a category of network
There are currently 4 incidents with a category of request
There are currently 7 incidents with a category of software

Scoped GlideAggregate - addNotNullQuery(String fieldName)

Adds a not null query to the aggregate.

Table 8. Parameters
Name Type Description
fieldname String Name of the field.
Table 9. Returns
Type Description
GlideQueryCondition Scoped query condition.

Example

var count = new GlideAggregate('incident');
  count.addNotNullQuery('short_description');
  count.query();   // Issue the query to the database to get all records
  while (count.next()) {   
     // add code here to process the aggregate
  }

Scoped GlideAggregate - addNullQuery(String fieldName)

Adds a null query to the aggregate.

Table 10. Parameters
Name Type Description
fieldName String Name of the field.
Table 11. Returns
Type Description
GlideQueryCondition The scoped query condition.

Example

var count = new GlideAggregate('incident');
  count.addNullQuery('short_description');
  count.query();   // Issue the query to the database to get all records
  while (count.next()) {   
     // add code here to process the aggregate
  }

Scoped GlideAggregate - addTrend(String fieldName, String timeInterval, Number numUnits)

Adds a trend for a specified field. Use a trend to show patterns over a period of time.

Note: To control whether to group dayofweek results by year, use Scoped GlideAggregate - setIntervalYearIncluded(Boolean b).
Table 12. Parameters
Name Type Description
fieldName String Name of the field for which trending should occur.
timeInterval String Time interval for the trend.
Valid values:
  • date
  • dayofweek
  • hour
  • minute
  • quarter
  • value
  • week
  • year
numUnits Number Optional. Only valid when timeInterval = minute. Number of minutes to include in the trend.

Default: 1

Table 13. Returns
Type Description
void

Example

var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT'); // Count all incidents opened each quarter
ga.addTrend('opened_at', 'quarter');
ga.query();
while(ga.next()) {
 gs.info([ga.getValue('timeref'), ga.getAggregate('COUNT')]);
}
Output:
3/2018, 9
4/2018, 2
1/2019, 38
2/2019, 310

Scoped GlideAggregate - getAggregate(String agg, String name)

Returns the value of an aggregate from the current record.

Table 14. Parameters
Name Type Description
agg String Type of the aggregate.
Valid values:
  • AVG: Average value of the expression.
  • COUNT: Count of the number of non-null values.
  • GROUP_CONCAT: Concatenates all non-null values of the group in ascending order, joins them with a comma (','), and returns the result as a string.
  • GROUP_CONCAT_DISTINCT: Concatenates all non-null values of the group in ascending order, removes duplicates, joins them with a comma (','), and returns the result as a string.
  • MAX: Largest, or maximum, value.
  • MIN: Minimum value.
  • STDDEV: Population standard deviation.
  • SUM: Sum of all values.
name String Name of the field on which to perform the aggregation.
Table 15. Returns
Type Description
String The 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

Shows an aggregation that returns the number of records in the Incident table.

var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
  incidents = count.getAggregate('COUNT');
}
//Number of incidents varies depending on the current state
//of the incident table
gs.info('Number of incidents: ' + incidents);

Output: Number of incidents: 63.

Example

Shows the aggregation of an FX Currency field.

var ga = new GlideAggregate('laptop_tracker');
ga.addAggregate('SUM', 'cost');
ga.groupBy('name');
ga.query();
while (ga.next()) {
  gs.info('Aggregate results ' + ga.getValue('name') + ' => ' + ga.getAggregate('SUM', 'cost'));
}

Output:

*** Script: Aggregate results Apple MacBook Air => USD;1651.784280000000
*** Script: Aggregate results Apple MacBook Pro => USD;1651.784280000000
*** Script: Aggregate results Dell XPS => USD;470.852672000000
*** Script: Aggregate results LG =>
*** Script: Aggregate results Samsung Galaxy => USD;225.320000000000
*** Script: Aggregate results Surface3 => USD;2895.560369520000
*** Script: Aggregate results Toshiba => USD;9385.202875800000

Scoped GlideAggregate - getAggregateEncodedQuery()

Gets the query necessary to return the current aggregate.

Table 16. Parameters
Name Type Description
None
Table 17. Returns
Type Description
String Encoded query to get the aggregate.

Example

var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.groupBy('category');
count.query();  
while (count.next()) {
    gs.info(count.getAggregateEncodedQuery());
}
Output:
category=database
category=hardware
category=inquiry
category=network
category=request
category=software

Scoped GlideAggregate - getEncodedQuery()

Retrieves the encoded query.

Table 18. Parameters
Name Type Description
none
Table 19. Returns
Type Description
String Encoded query.

Example

var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();
gs.info(count.getEncodedQuery());
Output:
ORDERBYcategory^GROUPBYcategory

Scoped GlideAggregate - getRowCount()

Retrieves the number of rows in the GlideAggregate object.

Table 20. Parameters
Name Type Description
none
Table 21. Returns
Type Description
Number Number of rows in the GlideAggregate object.

Example

var count = new GlideAggregate('incident');
  count.addAggregate('MIN', 'sys_mod_count');
  count.addAggregate('MAX', 'sys_mod_count');
  count.addAggregate('AVG', 'sys_mod_count');
  count.groupBy('category');
  count.query();
  gs.info(count.getRowCount());
  while (count.next()) {  
     var min = count.getAggregate('MIN', 'sys_mod_count');
     var max = count.getAggregate('MAX', 'sys_mod_count');
     var avg = count.getAggregate('AVG', 'sys_mod_count');
     var category = count.category.getDisplayValue();
     gs.info(category + " Update counts: MIN = " + min + " MAX = " + max + " AVG = " + avg);
  }
Output:
6
Database Update counts: MIN = 8 MAX = 48 AVG = 28.0000
Hardware Update counts: MIN = 4 MAX = 14 AVG = 6.6250
Inquiry / Help Update counts: MIN = 0 MAX = 34 AVG = 6.5714
Network Update counts: MIN = 3 MAX = 37 AVG = 18.6000
Request Update counts: MIN = 5 MAX = 39 AVG = 13.4000
Software Update counts: MIN = 4 MAX = 98 AVG = 24.0000

Scoped GlideAggregate - getTableName()

Retrieves the table name associated with this GlideAggregate object.

Table 22. Parameters
Name Type Description
none
Table 23. Returns
Type Description
String Table name.

Example

var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();
gs.info(count.getTableName());

Scoped GlideAggregate - getValue(String name)

Returns the value of the specified field.

Table 24. Parameters
Name Type Description
name String Name of the field within the current table to return.
Table 25. Returns
Type Description
String Value of the specified field. Returns null if invalid (not part of result set).

Example

var count = new GlideAggregate('incident');
count.addAggregate('MAX', 'sys_mod_count');
count.groupBy('category');
count.query();   
while (count.next()) {  
  gs.info(count.getValue('category') + " category had " + count.getAggregate('MAX', 'sys_mod_count') + " updates");  
}

Output:

category had 12 updates
hardware category had 15 updates
inquiry category had 36 updates
network category had 37 updates
software category had 95 updates

Scoped GlideAggregate - groupBy(String name)

Provides the name of a field to use in grouping the aggregates.

May be called numerous times to set multiple group fields.

Table 26. Parameters
Name Type Description
name String Name of the field.
Table 27. Returns
Type Description
None

Example

var count = new GlideAggregate('incident');
count.addAggregate('MIN', 'sys_mod_count');
count.addAggregate('MAX', 'sys_mod_count');
count.addAggregate('AVG', 'sys_mod_count');
count.groupBy('category');
count.query();   
while (count.next()) {  
     var min = count.getAggregate('MIN', 'sys_mod_count');
     var max = count.getAggregate('MAX', 'sys_mod_count');
     var avg = count.getAggregate('AVG', 'sys_mod_count');
     var category = count.category.getDisplayValue();
     gs.info(category + " Update counts: MIN = " + min + " MAX = " + max + " AVG = " + avg);
}
Output:
Database Update counts: MIN = 8 MAX = 48 AVG = 28.0000
Hardware Update counts: MIN = 4 MAX = 14 AVG = 6.6250
Inquiry / Help Update counts: MIN = 0 MAX = 34 AVG = 6.5714
Network Update counts: MIN = 3 MAX = 37 AVG = 18.6000
Request Update counts: MIN = 5 MAX = 39 AVG = 13.4000
Software Update counts: MIN = 4 MAX = 98 AVG = 24.0000

Scoped GlideAggregate - hasNext()

Determines if there are any more records in the GlideAggregate object.

Table 28. Parameters
Name Type Description
none
Table 29. Returns
Type Description
Boolean

Flag that indicates whether there are more results in the query set.

Possible values:
  • true: More results are in the query set.
  • false: No more results in the query set.

Example

var agg = new GlideAggregate('incident');
agg.addAggregate('AVG', 'sys_mod_count');
agg.groupBy('category');
agg.query();
while (agg.hasNext()) {
    agg.next();
    var avg = agg.getAggregate('AVG', 'sys_mod_count');
    var category = agg.category.getDisplayValue();
    gs.info(category + ': AVG = ' + avg);
}
Output:
Database: AVG = 32.5000
Hardware: AVG = 12.0000
Inquiry / Help: AVG = 7.6667
Network: AVG = 24.0000
Request: AVG = 16.4000
Software: AVG = 27.0833

Scoped GlideAggregate - next()

Moves to the next record in the GlideAggregate.

Table 30. Parameters
Name Type Description
none
Table 31. Returns
Type Description
Boolean

Flag that indicates whether there are more results in the query set.

Possible values:
  • true: More results are in the query set.
  • false: No more results in the query set.

Example

var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
   incidents = count.getAggregate('COUNT');
   gs.info(incidents);
}

Scoped GlideAggregate - orderBy(String name)

Orders the aggregates using the value of the specified field. The field is also added to the group-by list.

Table 32. Parameters
Name Type Description
name String Name of the field to order the aggregates by.

Alternatively, you can provide a glidefunction to order the aggregates, such as glidefunction:length(short_description). For more information about glidefunctions, see glidefunction operations.

Table 33. Returns
Type Description
None

Example

var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category'); 
agg.orderBy('category'); 
agg.query(); 
while (agg.next()) { 
  var category = agg.category;
  var count = agg.getAggregate('count', 'category');
  var agg2 = new GlideAggregate('incident');   
  agg2.addAggregate('count', 'category');
  agg2.orderBy('category');
  gs.info(category + ": Current number of incidents:" + count);
}
Output:
database: Current number of incidents:2
hardware: Current number of incidents:8
inquiry: Current number of incidents:28
network: Current number of incidents:5
request: Current number of incidents:5
software: Current number of incidents:11

Scoped GlideAggregate - orderByAggregate(String agg, String fieldName)

Orders the aggregates based on the specified aggregate and field.

Table 34. Parameters
Name Type Description
agg String Type of aggregation.
fieldName String Name of the field to aggregate.
Table 35. Returns
Type Description
void

Example

ga.addAggregate('COUNT', 'category');
ga.orderByAggregate('count', 'category');
ga.query();
while(ga.next()) {
  gs.info('Category: ' + ga.category + ' ' + ga.getAggregate('COUNT', 'category'));
}

Output:

Category: inquiry 18
Category: software 11
Category: hardware 7
Category: network 5
Category: request 5
Category:  4
Category: database 2

Scoped GlideAggregate - orderByDesc(String name)

Sorts the aggregates in descending order based on the specified field. The field will also be added to the group-by list.

Table 36. Parameters
Name Type Description
name String Name of the field.
Table 37. Returns
Type Description
None

Example

var agg = new GlideAggregate('incident');
agg.addAggregate('count', 'category'); 
agg.orderByDesc('category'); 
agg.query(); 
while (agg.next()) { 
  var category = agg.category;
  var count = agg.getAggregate('count', 'category');
  var agg2 = new GlideAggregate('incident');   
  agg2.addAggregate('count', 'category');
  agg2.orderBy('category');
  gs.info(category + ": Current number of incidents:" + count);
}
Output:
software: Current number of incidents:11
request: Current number of incidents:5
network: Current number of incidents:5
inquiry: Current number of incidents:28
hardware: Current number of incidents:8
database: Current number of incidents:2

Scoped GlideAggregate - query()

Issues the query and gets the results.

Table 38. Parameters
Name Type Description
None
Table 39. Returns
Type Description
None

Example

var count = new GlideAggregate('incident');
count.addAggregate('COUNT');
count.query();
var incidents = 0;
if (count.next()) {
   incidents = count.getAggregate('COUNT');
}
gs.info('Number of incidents: ' + incidents);

Scoped GlideAggregate - setAggregateWindow(Number firstRow, Number lastRow)

Limits the number of rows from the table to include in the aggregate query.

Table 40. Parameters
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.
Table 41. Returns
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 GlideAggregate - setGroup(Boolean b)

Sets whether to group the return results.

Table 42. Parameters
Name Type Description
b Boolean

Flag that indicates whether to group the results.

Valid values:
  • true: Group the results.
  • false: Do not group the results.
Table 43. Returns
Type Description
void

Example

var ga = new GlideAggregate('incident');
ga.addAggregate('COUNT', 'category');
 
ga.setGroup(true);
ga.groupBy("category");
 
ga.query();
 
while(ga.next()) {
  gs.info('Category ' + ga.category + ' ' + ga.getAggregate('COUNT', 'category'));
  }

Output:

Category database 2
Category hardware 7
Category inquiry 18
Category network 5
Category request 5
Category software 11

Scoped 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: Scoped GlideAggregate - addTrend('<fieldName>', 'dayofweek').

Table 44. Parameters
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:
  • true: Group day-of-week results by year.
  • false: Exclude the year from time interval results.

Default: true

Table 45. Returns
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.

var incidentGroup = new GlideAggregate('incident');
incidentGroup.addEncodedQuery("sys_created_onRELATIVEGT@month@ago@6");
incidentGroup.addTrend('sys_created_on', 'dayofweek');
incidentGroup.addAggregate('COUNT');
incidentGroup.setIntervalYearIncluded(false);
incidentGroup.query();
while (incidentGroup.next()) {
    gs.info(incidentGroup.getValue('timeref') + ': ' + incidentGroup.getAggregate('COUNT'))};

Output:

Sunday: 1
Monday: 15
Tuesday: 1
Wednesday: 7
Thursday: 16
Saturday: 1