Sub topic 13
Cross-tabulated subtotals with GROUP BY CUBE
This chapter assumes you can write a basic aggregate query with GROUP BY and that you have seen GROUP BY ROLLUP. You should also be comfortable reading AggregateResult rows in Apex.
What CUBE adds to ROLLUP
GROUP BY ROLLUP(a, b) produces subtotals in one direction only. It returns rows for each a and b pair, a subtotal for each a, and a grand total. It does not return a subtotal for each b on its own.
GROUP BY CUBE(a, b) returns a subtotal for every combination of the grouped fields. With two fields, you get four kinds of rows:
- One row for each
aandbpair. - One subtotal row for each
a, withbaggregated away. - One subtotal row for each
b, withaaggregated away. - One grand total row, with both fields aggregated away.
This layout is called a cross-tabulation. Use it when you need to read the totals from both directions, such as leads by source and also leads by rating.
A CUBE query
SELECT LeadSource, Rating, COUNT(Id) cnt
FROM Lead
GROUP BY CUBE(LeadSource, Rating)Suppose the org has leads from Web and Phone, each rated Hot or Cold. The result includes the following kinds of rows. A subtotal row has null in the field that was aggregated away.
| LeadSource | Rating | cnt | Row type |
|---|---|---|---|
| Web | Hot | 10 | Detail |
| Web | null | 25 | Subtotal for Web |
| null | Hot | 18 | Subtotal for Hot |
| null | null | 60 | Grand total |
The number of row groups grows quickly. CUBE over n fields produces 2 to the power n grouping combinations. Keep the field list short.
Telling subtotals from real nulls
The GROUPING function solves the ambiguity. GROUPING(field) returns 1 when the field is aggregated away in that row, and 0 when the row contains a real value for it. A real value includes a true null.
SELECT LeadSource, Rating,
GROUPING(LeadSource) grpSource,
GROUPING(Rating) grpRating,
COUNT(Id) cnt
FROM Lead
GROUP BY CUBE(LeadSource, Rating)Read the two flags together:
| grpSource | grpRating | Meaning |
|---|---|---|
| 0 | 0 | Detail row |
| 0 | 1 | Subtotal for one LeadSource, all ratings |
| 1 | 0 | Subtotal for one Rating, all sources |
| 1 | 1 | Grand total |
The GROUPING function must refer to a field that appears in the CUBE list.
Reading CUBE results in Apex
Results come back as AggregateResult rows. Cast the GROUPING aliases to Integer, then branch on them.
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 (grpSource == 1) {
System.debug('Rating ' + ar.get('Rating') + ' across all sources: ' + cnt);
} else if (grpRating == 1) {
System.debug('Source ' + ar.get('LeadSource') + ' across all ratings: ' + cnt);
} else {
System.debug(ar.get('LeadSource') + ' / ' + ar.get('Rating') + ': ' + cnt);
}
}How the row types relate
Common mistakes
- Using
ROLLUPwhen you need subtotals for both fields.ROLLUPskips the second-field subtotals. - Treating every
nullas a subtotal. CheckGROUPINGinstead. - Adding many fields to
CUBE. Each added field doubles the number of grouping combinations, and the query is still subject to the governor limits on rows returned. - Selecting a
GROUPINGfield without giving it an alias. In Apex you then have to read it asexpr0,expr1, and so on.
Exercise
A CUBE query on LeadSource and Rating returns a row with LeadSource = null, Rating = 'Hot', grpSource = 1 and grpRating = 0. What does this row represent?
Show answer
It is a subtotal for the Hot rating across all lead sources. grpSource = 1 means LeadSource was aggregated away, and grpRating = 0 means Rating holds a real value.
Exercise
Why can you not rely on a null LeadSource alone to find the subtotal rows?
Show answer
Leads with an empty LeadSource are grouped as a real null value. The GROUPING function returns 0 for those rows and 1 only for rows where the field was aggregated away.