Sub topic 5
Naming Aggregate Results with Aliases
This chapter assumes you can write basic SOQL (SELECT, FROM, WHERE, ORDER BY, LIMIT) and read simple Apex loops and casts. You should also know how COUNT, SUM and GROUP BY work from the earlier chapters.
Why aliases matter
An alias is a name you give to an aggregate expression in the SELECT list. You write it right after the expression, separated by a space. There is no AS keyword in SOQL.
SELECT LeadSource, COUNT(Id) leadCount
FROM Lead
GROUP BY LeadSourceHere leadCount is the alias for COUNT(Id). Grouped fields such as LeadSource keep their own field names, so they need no alias.
Without an alias, Salesforce names each aggregate column automatically: expr0, expr1, and so on, in the order the expressions appear. That works, but expr1 tells you nothing when you read the code a month later.
Aliases and AggregateResult
In Apex, each row of an aggregate query comes back as an AggregateResult. You read a value with get('name'), where the name is the alias or the grouped field name.
for (AggregateResult ar : [
SELECT StageName, COUNT(Id) cnt, SUM(Amount) total
FROM Opportunity
GROUP BY StageName
]) {
String stage = (String) ar.get('StageName');
Integer cnt = (Integer) ar.get('cnt');
Decimal total = (Decimal) ar.get('total');
System.debug(stage + ': ' + cnt + ' opportunities, total ' + total);
}get returns an Object, so you must cast the result to the type you expect. The alias is the lookup key, so a typo in the string fails only at run time.
Without aliases, the same code needs ar.get('expr0') and ar.get('expr1'). Adding or reordering an aggregate would then silently shift those names.
Rules for alias names
- Put a space between the expression and the alias. Do not use
AS. - Pick a name that is not a reserved word or a field name used in the same query.
- Use the same spelling and capitalization in
get. Aliases are matched by name, so keep the two identical to avoid surprises. - Give every aggregate an alias when you read results in Apex. Aliases are optional but make the code safer.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Invalid field` error at run time from `get` | The alias in `get` does not match the alias in the query | Copy the alias exactly from the query |
| `System.TypeException` on cast | Wrong cast, such as `(Integer)` for a `SUM` of currency | Cast SUM and AVG to `Decimal`, and COUNT to `Integer` |
| Values appear in the wrong variables | You relied on `expr0` and `expr1`, then changed the SELECT list | Alias every aggregate |
| Syntax error near `AS` | Alias written with `AS` | Remove `AS` |
How aliases fit the query
The alias is the one name that links the query to the code. Keep it unique within the query and consistent with the get call.
Exercise
A query is SELECT AccountId, SUM(Amount), COUNT(Id) FROM Opportunity GROUP BY AccountId. Rewrite it so the sum is named total and the count is named cnt. Then write the Apex line that reads the sum from an AggregateResult named ar into a Decimal variable.
Show answer
SELECT AccountId, SUM(Amount) total, COUNT(Id) cnt FROM Opportunity GROUP BY AccountId. The Apex line is: Decimal total = (Decimal) ar.get('total');