'How to write the result of a BigQuery script to a table?

I have a BigQuery SQL script like this:

DECLARE my_dates STRING;

SET report_dates = (
  SELECT month FROM my_dataset.my_date_able)
);

EXECUTE IMMEDIATE format("""
SELECT * from
( select x,
         y,
         month,
         sum(things) as num_things
  FROM my_dataset.my_data
  GROUP BY 1,2,3
)
PIVOT
(
  sum(num_things) AS s
  FOR month in %s
)
""", my_dates);

What I would like to do is find a simple way to execute this script and write the results to another table but I can't figure it out. It seems I might need to create a table from a schema and then insert each call in EXECUTE IMMEDIATE separately - surely there's something more straightforward?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source