简体   繁体   中英

BigQuery - Using INSERT INTO to copy data from one nested table into another nested table

Helping a customer out. I'm trying to copy one nested BigQuery table into another nested table and am running into the following error: "Syntax error: Expected ")" or "," but got ".""

Query:

  INSERT INTO `<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table2` (id,
    first_name,
    last_name,
    dob,
    address.status,
    address.address,
    address.city,
    address.state,
    address.zip,
    address.numberOfYears)
SELECT
  id,
  first_name,
  last_name,
  dob,
  address.status,
  address.address,
  address.city,
  address.state,
  address.zip,
  address.numberOfYears
FROM
  `<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table`

Answer below. Hope this helps someone else out too!

INSERT INTO
  `<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table2` 
    (id,
    first_name,
    last_name,
    dob,
    addresses)
SELECT
  id,
  first_name,
  last_name,
  dob,
  ARRAY_AGG(STRUCT(a1.status,
      a1.address,
      a1.city,
      a1.state,
      a1.zip,
      a1.numberOfYears)) AS addresses
FROM
  `<GCP_PROJECT_NAME>.Test_Tables.Nested_Person_Table`,
  UNNEST(addresses) AS a1
GROUP BY
  id,
  first_name,
  last_name,
  dob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM