Scenario Sales managers review the open pipeline every Monday. They want the number and value of open Opportunities for each sales rep and each stage, with a subtotal per rep and a grand total, all in a single result.
Task
Write one SOQL query on Opportunity that:
IsClosed = false).GROUP BY ROLLUP(OwnerId, StageName) so the result contains per-owner-and-stage rows, a subtotal row for each owner, and one grand total row.Amount for every row.Expected output
Columns: OwnerId, StageName, cnt (number of Opportunities) and total (sum of Amount).
OwnerId and StageName filled in.StageName = null.OwnerId and StageName = null.
Row order is not checked.Example Open Opportunities: Ann has Prospecting 100 and 200, and Proposal 300. Ben has Prospecting 400.
Use one aggregate query on Opportunity with GROUP BY ROLLUP(OwnerId, StageName). Count only open Opportunities using IsClosed = false in WHERE. Alias the columns exactly as cnt and total. Row order is not checked.
7 row(s), any order
| # | cnt | total | OwnerId | StageName |
|---|---|---|---|---|
| 1 | 60 | 91500000 | null | null |
| 2 | 20 | 29500000 | 005000000000001AAA | Prospecting |
| 3 | 20 | 30500000 | 005000000000002AAA | Proposal |
| 4 | 20 | 31500000 | 005000000000003AAA | Negotiation/Review |
| 5 | 20 | 29500000 | 005000000000001AAA | null |
| 6 | 20 | 30500000 | 005000000000002AAA | null |
| 7 | 20 | 31500000 | 005000000000003AAA | null |