Pipeline Count by Stage
Intermediate SolvedA pipeline dashboard shows how many deals sit in each stage.
Return each StageName with the number of Opportunities in it, aliased total.
Sample data
Opportunity 6 records
| Name | StageName | Amount | CloseDate |
|---|---|---|---|
| A | Prospecting | 10,000 | today + 30 days |
| B | Prospecting | 20,000 | today + 30 days |
| C | Qualification | 30,000 | today + 30 days |
| D | Closed Won | 40,000 | today + 30 days |
| E | Closed Won | 50,000 | today + 30 days |
| F | Closed Won | 60,000 | today + 30 days |
Expected result on the sample
| StageName | total |
|---|---|
| Prospecting | 2 |
| Qualification | 1 |
| Closed Won | 3 |
Row order does not matter. Only the columns above are compared; extra columns in your SELECT are fine.
Hints
COUNT(Id) totalgives the count the aliastotal.- Every non-aggregated field in SELECT must be in GROUP BY.
What this tests
GROUP BY with COUNT(Id) does the counting in the database and returns one AggregateResult per stage, which you read in Apex with ar.get('total'). COUNT(Amount) looks similar but only counts rows where Amount is filled in. Aggregate queries also count every row they scan toward the 50,000-row limit, so they are not free on large objects.
Datasets
Solved on this device only. Sign in with Google to keep your progress across devices.
Run on sample executes your query against the sample data shown on the left and displays the rows it returns. Submit checks it against the sample plus 2 hidden datasets, comparing your rows with the expected rows on each. Queries run in a real Salesforce org; the data is inserted for your run and rolled back afterwards.