Scenario Account executives want to find customers whose sales pipeline is spread across the sales process, not stuck in one stage. An Account that has Opportunities in several different stages is considered to have a broad, mature pipeline.
Task
Write one SOQL query on Opportunity that:
AccountId is null).AccountId.StageName.Amount, highest first.Expected output
One row per qualifying Account with three columns: AccountId, stages (the number of distinct stages) and total (the sum of Amount).
Example Account A has Opportunities in Prospecting (100), Proposal (200) and Closed Won (300). Account B has two Opportunities, both Prospecting (500 and 400). Account C has Prospecting (50) and Proposal (60).
Result: A with stages = 3 and total = 600.
Use a single query on Opportunity with GROUP BY AccountId. Count distinct stages with COUNT_DISTINCT(StageName) in HAVING. Exclude Opportunities that have no Account. Alias the columns exactly as stages and total.
7 row(s), order checked
| # | total | stages | AccountId |
|---|---|---|---|
| 1 | 7000000 | 4 | 001000000000001AAA |
| 2 | 6600000 | 4 | 001000000000007AAA |
| 3 | 6200000 | 4 | 001000000000006AAA |
| 4 | 5800000 | 4 | 001000000000005AAA |
| 5 | 5400000 | 4 | 001000000000004AAA |
| 6 | 5000000 | 4 | 001000000000003AAA |
| 7 | 4600000 | 4 | 001000000000002AAA |