0

I am new and not sure what I am doing wrong I am trying to create a temp file and I am getting a syntax error with the following query:

WITH trees_temp AS
(
    SELECT *   
    FROM
        (SELECT *
         FROM bigquery-public-data.new_york_trees.tree_census_1995
        JOIN bigquery-public-data.new_york_trees.tree_census_2005
             ON tree_census_1995.diameter = tree_census_2005.tree_dbh
        WHERE diameter > 30)
)
3
  • 1
    You have a CTE but no SELECT. Commented Apr 14, 2021 at 19:32
  • oh sorry Im learning I thought the SELECT after the WITH is what it needed? Sorry Commented Apr 14, 2021 at 20:01
  • . . The SELECT is used to define the CTE. Then there should be another SELECT that uses the CTE. Commented Apr 14, 2021 at 20:33

1 Answer 1

1

You're using a common table expression - meaning that temporary table (trees_temp) is only available in the query you're running. You'll need to follow your WITH statement with a SELECT statement to actually query it.

WITH trees_temp AS
(
 SELECT *
 FROM bigquery-public-data.new_york_trees.tree_census_1995
 JOIN bigquery-public-data.new_york_trees.tree_census_2005
    ON tree_census_1995.diameter = tree_census_2005.tree_dbh
 WHERE diameter > 30
)
SELECT * FROM trees_temp;

If you want to create a temporary table that can be used across different queries you can create a new table or a view and give it an expiration time. For example:

CREATE OR REPLACE TABLE my-dataset.trees_temp
OPTIONS(
  expiration_timestamp=TIMESTAMP "2021-04-15 00:00:00 UTC"
)
as 
(
 SELECT t95.*
 FROM bigquery-public-data.new_york_trees.tree_census_1995 as t95
 JOIN bigquery-public-data.new_york_trees.tree_census_2005 as t05
    ON t95.diameter = t05.tree_dbh
 WHERE diameter > 30
)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much Leigha and for the link!! that helped so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.