Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery SQL, append SQL query result to existing table

I'm using Google Cloud Platform BigQuery SQL.

I have a table [myTable] and I'm writing the following SQL. I want to append the result of this SQL to myTable, but all I have managed so far is to replace myTable every time I run the SQL.

Can someone tell me what is the SQL sentence for append the query results to an existing table?

CREATE OR REPLACE myTable  -- myTable will be replace every time...
SELECT
  id,
  parameter1,
  parameter2
FROM 
  third_party_managerd_table
like image 205
Ema Avatar asked Jan 21 '19 01:01

Ema


People also ask

How do I add data to an existing table in BigQuery?

You can append additional data to an existing table by performing a load-append operation or by appending query results to the table. For more information on appending to or overwriting a table when loading data, see the documentation for your source data format: Appending to or overwriting a table with Avro data.

When you run a query in BigQuery what happens to the results?

When you run a SQL query in BigQuery, it automatically creates, schedules and runs a query job. BigQuery runs query jobs in two modes: interactive (default) and batch. Interactive (on-demand) queries are executed as soon as possible, and these queries count towards concurrent rate limit and daily limit.

What is Upsert in BigQuery?

As mentioned earlier, BigQuery UPSERT means UPDATE+INSERT. It is essentially INSERT that behaves like an UPDATE in case of duplicates, so as to not violate any uniqueness constraints.


2 Answers

Below is for BigQuery Standard SQL

#standardSQL
INSERT `project.dataset.yourtable` (id, parameter1, parameter2) 
SELECT
  id,
  parameter1,
  parameter2
FROM 
  `project.dataset.third_party_managerd_table`   

See more about BigQuery DML here - https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax

like image 140
Mikhail Berlyant Avatar answered Oct 16 '22 23:10

Mikhail Berlyant


It can also be done by creating a new table with the new query and inserting it into "myTable"

CREATE OR REPLACE new_info
SELECT
  id,
  parameter1,
  parameter2
FROM 
  third_party_managerd_table;

INSERT INTO myTable
SELECT 
  n.*
FROM 
  new_info n;
like image 22
panky_is Avatar answered Oct 17 '22 00:10

panky_is