Snowflake OBJECT_AGG won’t choose between two values for the same metric
DISTINCT removes identical key-value pairs, not competing values. Inspect the rows at the report’s intended grain before building the object.
A SQL agent asked to return one object of metrics per region might generate OBJECT_AGG(metric_name, metric_value::VARIANT). That expression builds the object; it does not decide which value belongs under each key. If two rows in the Northeast group both use net_revenue but one says 120 and the other says 135, Snowflake documents a duplicate-field-key error. Adding DISTINCT does not fix that case: it removes pairs only when both the key and the value match. Snowflake’s OBJECT_AGG reference also says input pairs with a SQL NULL key or value are ignored.
Check the rows that feed the object
Consider a hypothetical report in which 120 is the preliminary number and 135 is the certified revision. If the prompt asks for certified metrics, filter to the certified revision before building the object. If it asks to compare both versions, the output needs to preserve the version, for example by returning one object per region and version. Using MAX(metric_value) would pick 135 in this example, but only by coincidence: certification is not a rule about which number is bigger.
For a first-pass diagnostic, run the following against the same input rows the object will see, after joins and report filters. It intentionally flags identical repeats as well as conflicting values, so it also catches duplication that DISTINCT could mask. The NULL filters align the check with the inputs Snowflake says OBJECT_AGG will consider; check missing values separately if they matter to the reader.
SELECT region, metric_name, COUNT(*) AS input_rows
FROM report_rows
WHERE metric_name IS NOT NULL
AND metric_value IS NOT NULL
GROUP BY region, metric_name
HAVING COUNT(*) > 1;
When a key comes back, inspect the contributing rows before changing the aggregate. A join that copied a row, two revisions of one metric, and two different definitions exposed under the same label can all trigger the same error but call for different fixes. The useful instruction to give the agent is not “make the object query pass.” It is “show which rows compete for each key, and use the report’s rule to resolve them before aggregation.” The example is illustrative; the duplicate and NULL behavior comes from Snowflake’s documented contract.
sources
- Snowflake SQL reference: OBJECT_AGGdocs.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