Snowflake GROUP BY can split a bucket when its alias matches a source column
A query can show two rows labeled Paid while grouping on the original payment statuses. Check the grouping key, not just the output label or grand total.
Ask a SQL agent for payment totals by reporting status, with both paid and settled counted as Paid. In Snowflake, a query can apply that mapping in the output and still group by the original statuses. The giveaway is two rows that both say Paid. The grand total can be correct even though the requested breakdown is wrong.
The displayed label is not always the grouping key
Snowflake’s GROUP BY reference documents the precedence rule: when an unqualified name matches both a source column and a SELECT alias, the source column wins. Its worked example demonstrates that the grouping can follow one field while the displayed label comes from another. Here is an illustrative payment fixture applying the same rule without relying on an arbitrary representative value:
WITH payments(status, amount) AS (
SELECT * FROM VALUES ('paid', 40), ('settled', 60), ('pending', 25)
)
SELECT CASE WHEN status IN ('paid', 'settled') THEN 'Paid'
ELSE status END AS status,
SUM(amount) AS total
FROM payments
GROUP BY status;
Under the documented rule, GROUP BY status binds to the input column. The expected result for this fixture, ignoring row order, is Paid: 40, Paid: 60, and pending: 25. Summing those rows gives 125, so a grand-total check alone would not catch the split. Those figures are derived from the fixture, not a production measurement.
Name the reporting bucket before aggregating
WITH payments(status, amount) AS (
SELECT * FROM VALUES ('paid', 40), ('settled', 60), ('pending', 25)
), classified AS (
SELECT CASE WHEN status IN ('paid', 'settled') THEN 'Paid'
ELSE status END AS reporting_status,
amount
FROM payments
)
SELECT c.reporting_status, SUM(c.amount) AS total
FROM classified AS c
GROUP BY c.reporting_status;
Now the outer query groups by the actual reporting column. The expected output is one Paid row totaling 100 and one pending row totaling 25. For an agent regression test, include at least two raw statuses that map to the same label and assert both the number of rows for that label and its total. A fixture with only one raw status per label won’t expose the mistake.
sources
- Snowflake SQL reference: GROUP BY and column-versus-alias precedencedocs.snowflake.com
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