Two special forms of grouping in SOQL produce subtotals and grand totals for the record groupings specified in the query. They are
GROUP BY ROLLUP and
GROUP BY CUBE, and they replace
GROUP BY syntax and support up to three grouped fields. These functions make it easier for developers to produce cross-tabular or pivot-style outputs common to reporting tools, where groups become the axes and aggregate values are the cells. The Force.com database calculates the totals and provides them in-line, in the results, eliminating the need to write Apex to postprocess the data.
Listing 5.6 demonstrates
GROUP BY ROLLUP to add subtotals to combinations of two fields:
Status__c appears first in ...