Semi-structured & modern warehouse SQL
Concept  ·  SQL & Querying

UNNEST is a deliberate one-to-many — parent-level metrics summed after exploding are multiplied by list length.

Exploding an array changes the table's grain from parent to parent×element — that's the point — but every parent column rides along, copied once per element, and any aggregate over those copies counts the copies. It's the P06 flagship in modern clothes, with one difference: here the fan-out was chosen, which makes it feel safer and land more often. The discipline is a grain sentence before the SUM.

See it break
panels
panelpricecolors
P130['red'
P218['green']
The trap
SELECT
  SUM(price) AS revenue
FROM
  (
    SELECT
      panel,
      price,
      UNNEST(colors) AS color
    FROM
      panels
  );
The fix
SELECT
  SUM(price) AS revenue
FROM
  panels;
The trap
78
The fix
48

The panel revenue summed to 78, not 48 — after UNNEST exploded each panel's color array, its price rode along once per color, so SUM multiplied it by the list length.

The rule

Aggregate at parent grain first and join back; element metrics on the exploded table, parent metrics off it.

Shows up elsewhere
Fan-out before aggregateJSON null vs missing key
Try one →Prove it in practice →