How to to insert data into the the arrays of a table (Table1) from a subquery (Table2) in BigQuery?
I tried using INSERT INTO but with no success, then I tried using UPDATE and the ARRAY_CONCAT() function, it worked, but I'm wondering if there isn't a better way to get the same result.
You can see the example below:
Table1
Using the following schema
CREATE OR REPLACE TABLE
`project.dataset.Table1` ( column1 string,
column2 TIMESTAMP,
column3 ARRAY<int64>,
column4 ARRAY<int64>)
With this data
INSERT INTO
`project.dataset.Table1` (column1,
column2,
column3,
column4)
VALUES
("rowtest1", CURRENT_TIMESTAMP(), [5], [5000]),
("rowtest2", CURRENT_TIMESTAMP(), [10], [10000]),
("rowtest2", CURRENT_TIMESTAMP(), [12], [12000])
Table2
Using the following schema
CREATE OR REPLACE TABLE
`project.dataset.Table2` ( column1 string,
column2 TIMESTAMP,
column3 int64,
column4 int64)
With this data
INSERT INTO
`project.dataset.Table2` (column1,
column2,
column3,
column4)
VALUES
("rowtest1", CURRENT_TIMESTAMP(), 7, 7000)
To insert the data from Table2 into the arrays column3 and column4 of Table1, only for the respective rows in both tables, that is: the row WHERE column1 = "rowtest1", I used the following DML and then repeated the same logic for column4.
UPDATE
`project.dataset.Table1` t1
SET
column3 = ARRAY_CONCAT(t1.column3, (
SELECT
[t2.column3]
FROM
`project.dataset.Table2` t2 WHERE t2.column1 = "rowtest1"))
WHERE
t1.column1 = "rowtest1"
Is there another way to get the same result?
