# Open Pipeline Rollup by Owner and Stage

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

Practice this problem interactively (write and run your SOQL against realistic data): https://www.forcechallenges.com/problems/open-pipeline-rollup-owner-stage

## Problem

**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:
1. Considers only **open** Opportunities (`IsClosed = false`).
2. Groups them with `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.
3. Returns the count of Opportunities and the sum of `Amount` for every row.

**Expected output**
Columns: `OwnerId`, `StageName`, `cnt` (number of Opportunities) and `total` (sum of Amount).
- A normal row has both `OwnerId` and `StageName` filled in.
- An owner subtotal row has `StageName` = null.
- The grand total row has both `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.
- (Ann, Prospecting) gives cnt 2, total 300.
- (Ann, Proposal) gives cnt 1, total 300.
- (Ann, null) subtotal gives cnt 3, total 600.
- (Ben, Prospecting) gives cnt 1, total 400.
- (Ben, null) subtotal gives cnt 1, total 400.
- (null, null) grand total gives cnt 4, total 1,000.

## Constraints

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.

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