MALFORMED_QUERY: MAX() and MIN() are not supported with GROUP BY ROLLUP
What does this error mean?
GROUP BY ROLLUP is a SOQL extension that adds subtotal rows to aggregate results — similar to SQL's WITH ROLLUP. However, Salesforce restricts which aggregate functions can be used with ROLLUP. Specifically, MAX() and MIN() are not supported in the same query as GROUP BY ROLLUP. Only COUNT(), COUNT_DISTINCT(), SUM(), and AVG() are supported with ROLLUP.
Common Causes
1. Combining MAX() / MIN() with GROUP BY ROLLUP
Writing SELECT MAX(Amount), StageName FROM Opportunity GROUP BY ROLLUP(StageName) — the MAX() function is incompatible with the ROLLUP grouping modifier.
How to Fix It
Solution 1: Run MAX() / MIN() in a separate query
Split the reporting logic into two queries — one with ROLLUP for totals/counts, and a separate query for max/min values.
// ❌ BAD — MAX() with ROLLUP is not supported
List<AggregateResult> bad = [
SELECT StageName, MAX(Amount) maxAmt
FROM Opportunity
GROUP BY ROLLUP(StageName)
];
// ✅ Query 1: ROLLUP with supported functions
List<AggregateResult> rollupResults = [
SELECT StageName, SUM(Amount) total, COUNT() cnt
FROM Opportunity
GROUP BY ROLLUP(StageName)
];
// ✅ Query 2: MAX / MIN without ROLLUP
List<AggregateResult> minMaxResults = [
SELECT StageName, MAX(Amount) maxAmt, MIN(Amount) minAmt
FROM Opportunity
GROUP BY StageName
];
Supported aggregate functions with GROUP BY ROLLUP
| Function | Works with ROLLUP? |
|---|---|
COUNT() | ✅ Yes |
COUNT_DISTINCT() | ✅ Yes |
SUM() | ✅ Yes |
AVG() | ✅ Yes |
MAX() | ❌ Not supported |
MIN() | ❌ Not supported |