I am not able to create temporary table in Bigquery
As mentioned by @Jaytiger names are not included in the CREATE TEMP TABLE
statement.
You can create temporary table to store the results of a query as follows:
CREATE OR REPLACE TEMP TABLE <table1> as (SELECT * FROM `<dataset>.<table2>`);
SELECT * from <table1>
You can follow this documentation for further examples on Temp tables.
temp tables faster than variable tables big query
Temporary tables give flexibility to make customized tables for data visualization, as per the analytics requirements. More so, the use-case of TEMP is in the local temporary tables, only visible to the current session.
Meanwhile, the WITH
clause acts as a temporary table, but it is actually a result of a subquery which can be used somewhere else.
The time difference that you get is because temporary tables use cache query results. This means that the query values are stored in the cache memory. That’s why it is faster to execute than the queries with the WITH
clause. Sometimes, when you run a duplicate query, BigQuery attempts to reuse cached results.
How to insert data from CTE to a Temp Table?
Use below instead
CREATE TEMP TABLE temp1 AS (
WITH xyz AS
(SELECT * FROM table1)
SELECT * FROM xyz INNER JOIN table2 on ...
);
BigQuery: Insert into temporary table
CREATE TEMP TABLE myNewTempTable AS
SELECT *
FROM `project.dataset.myTable`
How to create a Temporary table in Bigquery
As suggested by @Paul, You get this error because a CTE (i.e. the WITH statement) is only part of a query. It needs to be followed by another statement, usually a SELECT. You can also refer to this StackOverflow question where the same error has been discussed.
You can try the below code which I have modified accordingly to resolve the error.
WITH analysis_fall_2021_season AS
(SELECT
start_station_name,
end_station_name,
EXTRACT (DATE FROM started_at) AS start_date,
EXTRACT (DATE FROM ended_at) AS end_date,
EXTRACT (TIME FROM started_at) AS start_time,
EXTRACT (TIME FROM ended_at) AS end_time,
DATETIME_DIFF (ended_at,started_at, MINUTE) AS total_lenght,
member_casual
FROM
(SELECT
fall_analysis.ride_id,
fall_analysis.started_at,
fall_analysis.ended_at,
fall_analysis.start_station_name,
fall_analysis.end_station_name,
fall_analysis.member_casual
FROM
`ciclystic.cyclistic_seasonal_analysis.fall_202010` AS fall_analysis
INNER JOIN
`ciclystic.cyclistic_seasonal_analysis.fall_202011` AS fall_202011
ON
fall_analysis.member_casual = fall_202011.member_casual
INNER JOIN
`ciclystic.cyclistic_seasonal_analysis.fall_202012` AS fall_202012
ON
fall_analysis.member_casual = fall_202012.member_casual)
)
SELECT * FROM analysis_fall_2021_season;
Related Topics
When to Use Grouping Sets, Cube and Rollup
Should I Set Max Pool Size in Database Connection String? What Happens If I Don'T
How to Do a Find/Replace in T-Sql
Vb.Net Escape Reserved Keywords in SQL Statement
Remove Trailing Spaces and Update in Columns in SQL Server
Postgresql Query for Getting N-Level Parent-Child Relation Stored in a Single Table
Sqlserver: How to Sort Table Names Ordered by Their Foreign Key Dependency
Using Union and Count(*) Together in SQL Query
Postgresql - Using Subqueries with Alter Sequence Expressions
How to Check If Value Exists in Each Group (After Group By)
Efficiently Mapping One-To-Many Many-To-Many Database to Struct in Golang
SQL - Select Distinct Only on One Column
Is There an Agreed Ideal Schema for Tagging