Sub topic 15
How reports relate to SOQL aggregate queries
This chapter assumes you can build summary and matrix reports in Lightning Experience and can read a basic SOQL SELECT statement. You should also know what objects, fields, and groupings are.
Why compare the two
A summary or matrix report groups records and calculates totals. SOQL has the same ability through aggregate functions and GROUP BY. Seeing both side by side shows what a report does behind the scenes. It also helps you decide when a report is enough and when you need code.
Summary report as GROUP BY
Take an Opportunity summary report. It is grouped by Stage, with a sum of Amount and a record count. This query produces the same numbers:
SELECT StageName, SUM(Amount) total, COUNT(Id) cnt
FROM Opportunity
GROUP BY StageNameEach row of the result matches one group in the report. SUM(Amount) matches the Amount summary. COUNT(Id) matches Record Count.
Matrix report as two-field GROUP BY
A matrix report groups by rows and by columns. In SOQL, you list both fields in GROUP BY. This query returns the cells of a matrix with Stage as rows and Close Year as columns:
SELECT StageName, CALENDAR_YEAR(CloseDate) yr, SUM(Amount) total
FROM Opportunity
GROUP BY StageName, CALENDAR_YEAR(CloseDate)Each result row is one cell. The query returns a flat list, so you pivot it yourself if you want a grid.
Mapping the concepts
| Report feature | SOQL equivalent |
|---|---|
| Row grouping | `GROUP BY` field |
| Column grouping (matrix) | Second field in `GROUP BY` |
| Sum, Average, Min, Max | `SUM()`, `AVG()`, `MIN()`, `MAX()` |
| Record Count | `COUNT()` or `COUNT(Id)` |
| Report filter | `WHERE` clause |
| Filter on a summary value | `HAVING` clause |
| Date grouping by year | `CALENDAR_YEAR()` |
Formulas have no direct counterpart
Custom summary formulas work on grouped results. The formula below gives the average amount per group:
AMOUNT:SUM / RowCountIn SOQL, AVG(Amount) gives a similar value, but it ignores records where Amount is null. Dividing by the record count includes those records in the denominator. The two numbers can differ.
Some formulas have no simple SOQL form. This one shows the change from the previous group:
AMOUNT:SUM - PREVGROUPVAL(AMOUNT:SUM, CLOSE_DATE)SOQL has no function that reads the previous group. You would run the query and compute the difference in Apex or in another tool.
Differences to keep in mind
- A report respects the running user's sharing, field-level security, and the report type's joins. SOQL in Apex may run in system mode, depending on how the class is declared.
- A report type can include related objects with inner or outer join behavior. A SOQL query follows relationship paths you write yourself.
- A report shows a limited number of rows in the interface and has its own export and run limits. SOQL aggregate queries have governor limits, such as the cap on rows processed in a transaction.
- Joined reports have no single SOQL equivalent. Each block is like a separate query, and the common grouping lines the results up.
Checking your understanding
Exercise
A matrix report shows Opportunity Amount with Stage as rows and Close Year as columns. Which SOQL clause list reproduces its cells, and what shape is the result?
Show answer
Use GROUP BY StageName, CALENDAR_YEAR(CloseDate) with SUM(Amount). The result is a flat list with one row per Stage and year pair, not a grid.
Exercise
Why can AMOUNT:SUM / RowCount differ from AVG(Amount) in SOQL?
Show answer
AVG(Amount) skips records with a null Amount. Dividing the sum by the record count counts those records, so the denominator is larger and the result can be lower.