Google bigquery 如何在Bigquery中更新嵌套数组中的字段?

Google bigquery 如何在Bigquery中更新嵌套数组中的字段?,google-bigquery,Google Bigquery,我正在尝试更新一个包含几个字段的表,ARRAYSTRUCT。 我需要更新的字段在数组中,我在使其工作时遇到了问题 以下是两个表的布局: CREATE TABLE mydatset.orders ( order_id string, order_time timestamp, trans STRUCT < id string, amount INT64, accounts ARRAY<STRUCT <

我正在尝试更新一个包含几个字段的表,ARRAYSTRUCT。 我需要更新的字段在数组中,我在使其工作时遇到了问题

以下是两个表的布局:

CREATE TABLE mydatset.orders (
order_id  string,
order_time timestamp,
trans  STRUCT <
    id      string,
    amount      INT64,
    accounts      ARRAY<STRUCT <
          role      STRING ,
          account_id        STRING,
          region          STRING,
          amount      INT64> > >
)
如果重新定位表中存在阵列帐户中的任何帐户,则尝试更新该帐户的区域:

update mydataset.orders a
set trans = (SELECT AS STRUCT   trans.* REPLACE(ARRAY(SELECT STRUCT<role STRING, account_id STRING, region STRING, amount INT64>
                          (cp.role,  cp.account_id,
                           case when cp.account_id = ll.account_id then ll.region else cp.region end ,
                           cp.amount
                          )
                    ) as accounts )
    from unnest(trans.accounts) cp
    left join unnest(relocs.chgs) ll
    on cp.account_id = ll.account_id
    )
from (select array_agg(struct (account_id, region) ) chgs
      from`mydataset.relocations`
      ) relocs
where true
语法正常,但sql不会执行预期的更新。运行上述更新后,不会更改订单表中的帐户区域

我已经看过了,这个案例略有不同。数组位于一个结构内部,它本身就是一个结构数组


感谢您的帮助。

下面是关于BigQuery标准SQL的

#standardSQL
UPDATE `project.dataset.orders`
SET trans = (SELECT AS STRUCT trans.* REPLACE(
  ARRAY(SELECT AS STRUCT x.* REPLACE(IFNULL(y.region, x.region) AS region)
    FROM UNNEST(trans.accounts) x
    LEFT JOIN UNNEST(relocations) y
    USING(account_id)
  ) AS accounts))
FROM (SELECT ARRAY_AGG(t) relocations FROM `project.dataset.relocations` t)
WHERE TRUE
使用以下虚拟数据对其进行测试

如下所示的初始虚拟数据

[
  {
    "order_id": "order_id1",
    "order_time": "2019-06-28 01:05:16.346854 UTC",
    "trans": {
      "id": "id1",
      "amount": "1",
      "accounts": [
        {
          "role": "role1",
          "account_id": "account_id1",
          "region": "region1",
          "amount": "11"
        },
        {
          "role": "role2",
          "account_id": "account_id2",
          "region": "region2",
          "amount": "12"
        }
      ]
    }
  },
  {
    "order_id": "order_id2",
    "order_time": "2019-06-28 01:05:16.346854 UTC",
    "trans": {
      "id": "id2",
      "amount": "1",
      "accounts": [
        {
          "role": "role3",
          "account_id": "account_id1",
          "region": "region4",
          "amount": "13"
        },
        {
          "role": "role4",
          "account_id": "account_id3",
          "region": "region3",
          "amount": "14"
        }
      ]
    }
  }
]
应用以下调整后

[
  {
    "account_id": "account_id1",
    "region": "regionA"
  },
  {
    "account_id": "account_id2",
    "region": "regionB"
  }
]   
结果是

[
  {
    "id": "id1",
    "amount": "1",
    "accounts": [
      {
        "role": "role1",
        "account_id": "account_id1",
        "region": "regionA",
        "amount": "11"
      },
      {
        "role": "role2",
        "account_id": "account_id2",
        "region": "regionB",
        "amount": "12"
      }
    ]
  },
  {
    "id": "id2",
    "amount": "1",
    "accounts": [
      {
        "role": "role3",
        "account_id": "account_id1",
        "region": "regionA",
        "amount": "13"
      },
      {
        "role": "role4",
        "account_id": "account_id3",
        "region": "region3",
        "amount": "14"
      }
    ]
  }
]

下面是BigQuery标准SQL

#standardSQL
UPDATE `project.dataset.orders`
SET trans = (SELECT AS STRUCT trans.* REPLACE(
  ARRAY(SELECT AS STRUCT x.* REPLACE(IFNULL(y.region, x.region) AS region)
    FROM UNNEST(trans.accounts) x
    LEFT JOIN UNNEST(relocations) y
    USING(account_id)
  ) AS accounts))
FROM (SELECT ARRAY_AGG(t) relocations FROM `project.dataset.relocations` t)
WHERE TRUE
使用以下虚拟数据对其进行测试

如下所示的初始虚拟数据

[
  {
    "order_id": "order_id1",
    "order_time": "2019-06-28 01:05:16.346854 UTC",
    "trans": {
      "id": "id1",
      "amount": "1",
      "accounts": [
        {
          "role": "role1",
          "account_id": "account_id1",
          "region": "region1",
          "amount": "11"
        },
        {
          "role": "role2",
          "account_id": "account_id2",
          "region": "region2",
          "amount": "12"
        }
      ]
    }
  },
  {
    "order_id": "order_id2",
    "order_time": "2019-06-28 01:05:16.346854 UTC",
    "trans": {
      "id": "id2",
      "amount": "1",
      "accounts": [
        {
          "role": "role3",
          "account_id": "account_id1",
          "region": "region4",
          "amount": "13"
        },
        {
          "role": "role4",
          "account_id": "account_id3",
          "region": "region3",
          "amount": "14"
        }
      ]
    }
  }
]
应用以下调整后

[
  {
    "account_id": "account_id1",
    "region": "regionA"
  },
  {
    "account_id": "account_id2",
    "region": "regionB"
  }
]   
结果是

[
  {
    "id": "id1",
    "amount": "1",
    "accounts": [
      {
        "role": "role1",
        "account_id": "account_id1",
        "region": "regionA",
        "amount": "11"
      },
      {
        "role": "role2",
        "account_id": "account_id2",
        "region": "regionB",
        "amount": "12"
      }
    ]
  },
  {
    "id": "id2",
    "amount": "1",
    "accounts": [
      {
        "role": "role3",
        "account_id": "account_id1",
        "region": "regionA",
        "amount": "13"
      },
      {
        "role": "role4",
        "account_id": "account_id3",
        "region": "region3",
        "amount": "14"
      }
    ]
  }
]

谢谢你的回复。奇怪的是,当我尝试将您的查询作为仅替换数据集名称的查询时,会出现以下错误:不支持引用其他表的相关子查询,除非它们可以取消相关,例如通过将它们转换为有效联接。我想这就是为什么我最初使用array_agg来处理relocations table.argh中的数据。我主要集中在调整您的查询,假设它在核心是正确的。让我再检查一下,我会回来的shortly@FZF-见更新的答案-现在已经测试-所以肯定对你有用:oThank you for your response。奇怪的是,当我尝试将您的查询作为仅替换数据集名称的查询时,会出现以下错误:不支持引用其他表的相关子查询,除非它们可以取消相关,例如通过将它们转换为有效联接。我想这就是为什么我最初使用array_agg来处理relocations table.argh中的数据。我主要集中在调整您的查询,假设它在核心是正确的。让我再检查一下,我会回来的shortly@FZF-见更新的答案-现在已经测试-所以肯定对你有用:o