Scenario Marketing wants to know which lead sources produce the biggest paying customers. Small deals and one-off sources would distort the picture, so only meaningful volumes of large won deals should be compared.
Task
Write one SOQL query on Opportunity that:
IsWon = true) with an Amount greater than 100,000.LeadSource.LeadSource ascending.Expected output
One row per lead source with four columns: LeadSource, deals (number of qualifying deals), accts (number of distinct Accounts behind those deals) and avgAmt (average Amount).
Example Qualifying won deals: Web 300,000 (Acme), Web 500,000 (Acme), Web 700,000 (Globex), Partner 200,000 (Acme), Partner 400,000 (Initech), Trade Show 900,000 (Acme).
Result: Web / deals 3 / accts 2 / avgAmt 500,000.
Use one aggregate query on Opportunity: WHERE for the won and amount filters, GROUP BY LeadSource, HAVING on the deal count, COUNT_DISTINCT for accounts and AVG for the average. Alias the columns exactly as deals, accts and avgAmt. Order by average amount descending, then LeadSource.
3 row(s), order checked
| # | accts | deals | avgAmt | LeadSource |
|---|---|---|---|---|
| 1 | 10 | 10 | 990000 | Referral |
| 2 | 9 | 9 | 960000 | Web |
| 3 | 10 | 10 | 930000 | Partner |