Why a pivot grand total for distinct customers is not the sum of its cells
Metabase’s engineering write-up shows how the database computes each subtotal separately. A three-row example gives analytics agents a test for double-counting.
An analytics agent asked for “unique buyers by channel, plus the grand total” should not add the channel counts. A buyer who ordered through two channels belongs in both counts, but only once in the grand total. Metabase’s engineering write-up shows the distinction in the SQL: detail cells and totals are separate aggregations, even when the database returns them in one result.
Three rows are enough to test it
Suppose customer A bought through Web and Store, and customer B bought through Web. The Web cell is 2 and the Store cell is 1. Adding the cells gives 3, but the overall distinct count is 2. Neither channel cell needs to change to make the grand total correct. If the agent only receives those two counts, it cannot recover the answer: Web could have two customers and Store a third, which would make the total 3 with the same visible cells.
The following illustrative query applies the same grouping-set pattern to a distinct count. In PostgreSQL’s documented semantics, each grouping set aggregates the input independently. The empty set () groups all qualifying rows together; GROUPING(channel) distinguishes the grand-total row from a detail group whose channel is actually null.
WITH orders(channel, customer_id) AS (
VALUES ('Web', 'A'), ('Store', 'A'), ('Web', 'B')
)
SELECT channel, GROUPING(channel) AS is_total,
COUNT(DISTINCT customer_id) AS unique_buyers
FROM orders
GROUP BY GROUPING SETS ((channel), ());
Ask the engine for the total
Metabase’s engineering post describes how its version 64 native pivot path compiles the detail, subtotal and grand-total aggregations into one statement when the feature is enabled and the driver supports it. The setting was off by default in v64. The earlier path issued separate queries for those levels. The distinction matters regardless of which path runs: neither requires the grand total to be the sum of displayed cells. The Metabase pivot documentation also distinguishes a query-builder pivot from a native SQL question: if you start with a SQL question that has no parameters, save it first and build a query-builder question on top of it. For this metric, keep customer IDs in that input rather than saving only the channel counts.
For a text-to-SQL acceptance test, put one buyer in two groups and require the agent to explain why the grand total is lower than the sum. If it has only the displayed counts, the right response is to ask for the underlying rows or a separately computed total, not to guess how much the groups overlap.
sources
- Metabase engineering: Making pivot tables faster with GROUPING SETSwww.metabase.com
- Metabase documentation: Pivot tableswww.metabase.com
- PostgreSQL documentation: GROUPING SETS, CUBE, and ROLLUPwww.postgresql.org
comments · 0
- EKOS’s pending BIRD submission pairs a SQLite-tuned run with a separate hidden-test scoreDec 3, 2026
- MotherDuck rolls out DuckDB 2.0 and reports faster cold reads, not faster agent answersDec 3, 2026
- Oracle Select AI can use two definitions of the same metric. Source order won’t break the tieDec 3, 2026