Sub topic 14
Identifying subtotal rows with GROUPING
This chapter assumes you can write GROUP BY ROLLUP and GROUP BY CUBE queries and read AggregateResult rows in Apex. It also assumes you know that subtotal rows return null for the fields that were rolled up.
Why null is not enough
A subtotal row returns null in the field it summarizes. A detail row can also hold null when the record has no value. A lead with no LeadSource forms its own group with LeadSource = null. You can't tell that row from a subtotal row by looking at the field.
The GROUPING function removes the ambiguity. GROUPING(field) returns 1 when the row is a subtotal that aggregates that field away. It returns 0 when the field is part of the row's grouping, even if its value is null.
Using GROUPING in a query
GROUPING accepts only a field that appears in the ROLLUP or CUBE list. Give each call an alias so you can read it in Apex.
SELECT LeadSource, Rating,
GROUPING(LeadSource) grpSource,
GROUPING(Rating) grpRating,
COUNT(Id) cnt
FROM Lead
GROUP BY CUBE(LeadSource, Rating)Each row falls into one of four kinds, depending on the two flags.
| grpSource | grpRating | Meaning of the row |
|---|---|---|
| 0 | 0 | Detail row for one LeadSource and Rating pair |
| 0 | 1 | Subtotal for one LeadSource across all ratings |
| 1 | 0 | Subtotal for one Rating across all sources (CUBE only) |
| 1 | 1 | Grand total |
ROLLUP never produces the 1, 0 row, because it only subtotals from the right-most field inward.
Filtering with HAVING
You can use GROUPING in HAVING to keep only certain row kinds. This query returns only the per-source subtotals from a ROLLUP. It excludes detail rows and the grand total.
SELECT LeadSource, Rating,
GROUPING(LeadSource) grpSource,
GROUPING(Rating) grpRating,
COUNT(Id) cnt
FROM Lead
GROUP BY ROLLUP(LeadSource, Rating)
HAVING GROUPING(LeadSource) = 0 AND GROUPING(Rating) = 1Reading the flags in Apex
The flag comes back as an Integer in the AggregateResult. Branch on it instead of testing for null.
for (AggregateResult ar : [
SELECT LeadSource, Rating,
GROUPING(LeadSource) grpSource,
GROUPING(Rating) grpRating,
COUNT(Id) cnt
FROM Lead
GROUP BY CUBE(LeadSource, Rating)
]) {
Integer grpSource = (Integer) ar.get('grpSource');
Integer grpRating = (Integer) ar.get('grpRating');
Integer cnt = (Integer) ar.get('cnt');
if (grpSource == 1 && grpRating == 1) {
System.debug('Grand total: ' + cnt);
} else if (grpRating == 1) {
System.debug('Subtotal for source ' + ar.get('LeadSource') + ': ' + cnt);
} else if (grpSource == 1) {
System.debug('Subtotal for rating ' + ar.get('Rating') + ': ' + cnt);
} else {
System.debug('Detail: ' + ar.get('LeadSource') + ' / ' + ar.get('Rating') + ': ' + cnt);
}
}Common errors
- Calling
GROUPINGon a field that is not in theROLLUPorCUBElist. The query fails. - Using
GROUPINGwith a plainGROUP BY. It only applies toROLLUPandCUBE. - Leaving out the alias, then reading the value with a guessed name such as
expr0. Alias everyGROUPINGcall. - Casting the flag to
Boolean. It is anInteger.
Exercise
A ROLLUP(LeadSource, Rating) query returns a row with LeadSource = null, Rating = null, and both GROUPING values 0. What does the row represent?
Show answer
A detail row for leads that have no LeadSource and no Rating. Both fields are part of the grouping, so both flags are 0. A grand total would have both flags set to 1.