# Quarterly Won Revenue

**Type:** SOQL · **Difficulty:** Hard · **Topics:** Date Functions, GROUP BY, Aggregates

Practice this problem interactively (write and run your SOQL against realistic data): https://www.forcechallenges.com/problems/quarterly-won-revenue

## Problem

**Scenario**
The finance team reports revenue to the board every quarter. Only deals that were actually won count as revenue, and the quarter is determined by the date the deal closed, not when it was created.

**Task**
Write one SOQL query on `Opportunity` that:
1. Considers only **won** Opportunities (`IsWon = true`).
2. Groups them by the calendar **year** and calendar **quarter** of `CloseDate`.
3. Sums `Amount` for each year and quarter.
4. Sorts the result chronologically: year ascending, then quarter ascending.

**Expected output**
One row per quarter that has won revenue, with three columns: `yr` (calendar year), `qtr` (quarter number 1 to 4) and `revenue` (sum of `Amount`). Quarters with no won deals do not appear.

**Example**
Won deals: 100,000 closed on 10 Feb 2025, 50,000 closed on 20 Mar 2025, 70,000 closed on 5 Jul 2025. A 90,000 deal closed on 1 Feb 2025 was Closed Lost.
- 2025 Q1 revenue is 150,000 (the lost deal is ignored).
- 2025 Q3 revenue is 70,000.
- 2025 Q2 has no won deals, so it is not listed.

Result: 2025 / 1 / 150,000, then 2025 / 3 / 70,000.

## Constraints

Use one aggregate query on Opportunity. Filter with IsWon = true. Group by CALENDAR_YEAR(CloseDate) and CALENDAR_QUARTER(CloseDate), and order by the same two expressions ascending. Alias the columns exactly as `yr`, `qtr` and `revenue`.

---
Source: ForceChallenges · last updated 2026-10-24
