What is GlideAggregate?

GlideAggregate performs database aggregation queries like COUNT, SUM, AVG, MIN, and MAX. It's optimized for statistical calculations.

Performance

GlideAggregate is more efficient than looping through records with GlideRecord for counts and calculations. Use it for reporting and analytics.

COUNT - Counting Records

Basic COUNT Example
// Count active incidents
var ga = new GlideAggregate('incident');
ga.addQuery('active', true);
ga.addAggregate('COUNT');
ga.query();

if (ga.next()) {
    var count = ga.getAggregate('COUNT');
    gs.info('Active incidents: ' + count);
}

// Count by priority
var ga2 = new GlideAggregate('incident');
ga2.addQuery('active', true);
ga2.groupBy('priority');
ga2.addAggregate('COUNT');
ga2.query();

while (ga2.next()) {
    var priority = ga2.priority.toString();
    var count = ga2.getAggregate('COUNT');
    gs.info('Priority ' + priority + ': ' + count + ' incidents');
}

SUM, AVG, MIN, MAX

Numeric Aggregations
// SUM - Total cost of all hardware
var ga = new GlideAggregate('alm_hardware');
ga.addAggregate('SUM', 'cost');
ga.query();

if (ga.next()) {
    var totalCost = ga.getAggregate('SUM', 'cost');
    gs.info('Total hardware cost: $' + totalCost);
}

// AVG - Average resolution time
var ga2 = new GlideAggregate('incident');
ga2.addQuery('state', '6'); // Resolved
ga2.addAggregate('AVG', 'calendar_duration');
ga2.query();

if (ga2.next()) {
    var avgDuration = ga2.getAggregate('AVG', 'calendar_duration');
    gs.info('Average resolution time: ' + avgDuration + ' seconds');
}

// MIN and MAX - Age range
var ga3 = new GlideAggregate('incident');
ga3.addAggregate('MIN', 'opened_at');
ga3.addAggregate('MAX', 'opened_at');
ga3.query();

if (ga3.next()) {
    var oldest = ga3.getAggregate('MIN', 'opened_at');
    var newest = ga3.getAggregate('MAX', 'opened_at');
    gs.info('Oldest: ' + oldest + ', Newest: ' + newest);
}

GROUP BY - Grouping Results

Grouping and Aggregating
// Count incidents by assignment group
var ga = new GlideAggregate('incident');
ga.addQuery('active', true);
ga.groupBy('assignment_group');
ga.addAggregate('COUNT');
ga.orderByAggregate('COUNT'); // Order by count
ga.query();

gs.info('Incidents by Group:');
while (ga.next()) {
    var group = ga.assignment_group.getDisplayValue();
    var count = ga.getAggregate('COUNT');
    gs.info(group + ': ' + count);
}

// Multiple grouping - by state and priority
var ga2 = new GlideAggregate('incident');
ga2.groupBy('state');
ga2.groupBy('priority');
ga2.addAggregate('COUNT');
ga2.query();

while (ga2.next()) {
    var state = ga2.state.getDisplayValue();
    var priority = ga2.priority.toString();
    var count = ga2.getAggregate('COUNT');
    gs.info('State: ' + state + ', Priority: ' + priority + ', Count: ' + count);
}

HAVING Clause

Filtering Aggregated Results
// Groups with more than 10 incidents
var ga = new GlideAggregate('incident');
ga.addQuery('active', true);
ga.groupBy('assignment_group');
ga.addAggregate('COUNT');
ga.addHaving('COUNT', '>', 10);
ga.query();

gs.info('Groups with >10 incidents:');
while (ga.next()) {
    var group = ga.assignment_group.getDisplayValue();
    var count = ga.getAggregate('COUNT');
    gs.info(group + ': ' + count);
}

// Users with average resolution time > 2 days
var ga2 = new GlideAggregate('incident');
ga2.addQuery('state', '6');
ga2.groupBy('assigned_to');
ga2.addAggregate('AVG', 'calendar_duration');
ga2.addHaving('AVG', 'calendar_duration', '>', 172800); // 2 days in seconds
ga2.query();

while (ga2.next()) {
    var user = ga2.assigned_to.getDisplayValue();
    var avgTime = ga2.getAggregate('AVG', 'calendar_duration');
    gs.info(user + ': ' + avgTime + ' seconds average');
}

Common Patterns

Practical Examples
// Pattern 1: Get total count
function getTotalIncidents() {
    var ga = new GlideAggregate('incident');
    ga.addAggregate('COUNT');
    ga.query();
    return ga.next() ? parseInt(ga.getAggregate('COUNT')) : 0;
}

// Pattern 2: Count by field value
function countByPriority() {
    var results = {};
    var ga = new GlideAggregate('incident');
    ga.groupBy('priority');
    ga.addAggregate('COUNT');
    ga.query();
    
    while (ga.next()) {
        var priority = ga.priority.toString();
        results[priority] = parseInt(ga.getAggregate('COUNT'));
    }
    return results;
}

// Pattern 3: Top N records
function getTopAssignees(limit) {
    var ga = new GlideAggregate('incident');
    ga.groupBy('assigned_to');
    ga.addAggregate('COUNT');
    ga.orderByAggregate('COUNT');
    ga.setLimit(limit);
    ga.query();
    
    var results = [];
    while (ga.next()) {
        results.push({
            user: ga.assigned_to.getDisplayValue(),
            count: ga.getAggregate('COUNT')
        });
    }
    return results;
}

CSA & CAD Exam Questions (10 Questions)

What is GlideAggregate used for? Q1
CSA
  • A) Updating multiple records
  • B) Database aggregation queries (COUNT, SUM, AVG)
  • C) Creating records
  • D) Deleting records
Show Answer
Correct Answer: B
GlideAggregate performs database aggregation queries like COUNT, SUM, AVG, MIN, and MAX. It's optimized for statistics and reporting.
How do you count records using GlideAggregate? Q2
CSA
  • A) ga.count()
  • B) ga.addAggregate('COUNT')
  • C) ga.getCount()
  • D) ga.addCount()
Show Answer
Correct Answer: B
Use ga.addAggregate('COUNT') to count records, then retrieve with ga.getAggregate('COUNT') after querying.
What method groups results in GlideAggregate? Q3
CSA
  • A) ga.group('field')
  • B) ga.groupBy('field')
  • C) ga.addGroup('field')
  • D) ga.setGroup('field')
Show Answer
Correct Answer: B
Use groupBy('field_name') to group results. You can call it multiple times for multiple grouping levels.
How do you retrieve an aggregated value? Q4
CSA
  • A) ga.getValue('COUNT')
  • B) ga.getAggregate('COUNT')
  • C) ga.aggregate('COUNT')
  • D) ga.count
Show Answer
Correct Answer: B
Use getAggregate('FUNCTION') or getAggregate('FUNCTION', 'field') to retrieve aggregated values after calling next().
Which aggregation calculates average values? Q5
CAD
  • A) MEAN
  • B) AVG
  • C) AVERAGE
  • D) MEDIAN
Show Answer
Correct Answer: B
Use AVG to calculate averages: ga.addAggregate('AVG', 'field_name'). Then retrieve with getAggregate('AVG', 'field_name').
What does addHaving() do? Q6
CAD
  • A) Adds a query condition
  • B) Filters aggregated results
  • C) Adds a field to results
  • D) Includes related records
Show Answer
Correct Answer: B
addHaving() filters aggregated results (like SQL HAVING clause). Example: ga.addHaving('COUNT', '>', 10) returns only groups with count > 10.
Can you loop through GlideAggregate results like GlideRecord? Q7
CSA
  • A) No, different structure
  • B) Yes, use while (ga.next())
  • C) Only with forEach()
  • D) Only with for loop
Show Answer
Correct Answer: B
Yes, use while (ga.next()) to loop through aggregated results, just like GlideRecord. Each iteration represents a group.
How do you order results by the aggregated value? Q8
CAD
  • A) ga.orderBy('COUNT')
  • B) ga.orderByAggregate('COUNT')
  • C) ga.sortAggregate('COUNT')
  • D) ga.sort('COUNT')
Show Answer
Correct Answer: B
Use orderByAggregate('FUNCTION') to order by aggregated values. Useful for finding top/bottom performers.
Which is more efficient for counting records? Q9
CAD
  • A) GlideRecord with getRowCount()
  • B) GlideAggregate with COUNT
  • C) Both are equal
  • D) Depends on the table
Show Answer
Correct Answer: B
GlideAggregate is more efficient for counting and calculations because it performs aggregation at the database level without loading full records.
Can GlideAggregate modify records? Q10
CSA
  • A) Yes, like GlideRecord
  • B) No, it's read-only for statistics
  • C) Only with admin role
  • D) Only for numeric fields
Show Answer
Correct Answer: B
GlideAggregate is read-only and used only for statistical queries. Use GlideRecord for creating, updating, or deleting records.

Aggregate Functions

  • COUNT - Count records
  • SUM - Total values
  • AVG - Average values
  • MIN - Minimum value
  • MAX - Maximum value
  • groupBy() - Group results
  • addHaving() - Filter groups
  • Read-only, optimized queries