title: Unnesting structs in BigQuery
body: <p>What is the correct way to flatten a struct of two arrays in BigQuery? I have a dataset like the one pictured here (the struct.destination and struct.visitors arrays are ordered - i.e. the visitor counts correspond specifically to the destinations in the same row):</p>
  
  <p><a href="https://i.stack.imgur.com/VQndq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/VQndq.png" alt="enter image description here"></a></p>
  
  <p>I want to reorganize the data so that I have a total visitor count for each unique combination of origins and destinations. Ideally, the end result will look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/xyOyB.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xyOyB.png" alt="enter image description here"></a></p>
  
  <p>I tried using UNNEST twice in a row - once on struct.destination and then on struct.visitors, but this produces the wrong result (each destination gets mapped to every value in the array of visitor counts when it should only get mapped to the value in the same row):</p>
  
  <pre><code>SELECT
    origin,
    unnested_destination,
    unnested_visitors
  FROM
    dataset.table,
    UNNEST(struct.destination) AS unnested_destination,
    UNNEST(struct.visitors) AS unnested_visitors
  </code></pre>