'Multiple WITH statements and CREATE TABLE, the correct way to create a table

A challenge I have is how to create a table, but from previous operations that manipulate other tables, but I seem unable to create the table at the last step. For example, my queries are the following:

 WITH recs as (
    SELECT product_id
    FROM `table1`,
 ),
 filtered as (
    select * from recs where product_id is not null
 ),
 
 CREATE OR REPLACE TABLE `newtable` AS
 (
    select * from recs where product_id is not null
 )

I'm getting the error:

Syntax error: Expected "(" or "," or keyword SELECT but got keyword CREATE at [21:5]


Solution 1:[1]

use below instead

CREATE OR REPLACE TABLE `newtable` AS (
  WITH recs as (
    SELECT product_id
    FROM `table1`
  ), filtered as (
    select * from recs where product_id is not null
  )
  select * from recs where product_id is not null
)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Mikhail Berlyant