如何根据 BQ 中的嵌套字段值更新顶级列

How to update a top level column based on nested field value in BQ

我正在尝试使用下面的 case 语句基于嵌套列值更新顶级列

UPDATE
  `project.database.table`
SET
  ReplaceTotal= (
  SELECT
    CASE
      WHEN DamageLineInfo.MessageCode='MO' AND DamageLineInfo.AutomatedEntry=TRUE AND DamageLineInfo.LaborInfo.LaborOperation='OPO' AND DamageLineInfo.LaborInfo.LaborType='LAB' AND DamageLineInfo.LaborInfo.LaborInclInd=FALSE THEN DamageLineInfo.LaborInfo.LaborAmt
      WHEN DamageLineInfo.LaborInfo.LaborOperation='OP9'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP11'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP5'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP12'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP10'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP1'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP21' THEN
    CASE
      WHEN DamageLineInfo.LaborInfo.LaborType='LAB' AND DamageLineInfo.LaborInfo.LaborInclInd=FALSE THEN DamageLineInfo.LaborInfo.LaborAmt
  END
  END as ReplaceTotal
  FROM
    UNNEST(ServiceBusEnvelope.Payload.RepairOrderFolderAddRq.DamageLineInfo) AS DamageLineInfo)
WHERE
  TRUE

当我 运行 查询时出现以下错误 标量子查询产生了一个以上的元素

帮助我理解错误。 如果错误是因为生成的数据有值列表,那么请我获取数据的第一个值。

If the error is because the data generated is having list of values

是的,就是这个原因

get the first value of the data

只需添加 LIMIT 1,如下例所示

UPDATE
  `project.database.table`
SET
  ReplaceTotal= (
  SELECT
    CASE
      WHEN DamageLineInfo.MessageCode='MO' AND DamageLineInfo.AutomatedEntry=TRUE AND DamageLineInfo.LaborInfo.LaborOperation='OPO' AND DamageLineInfo.LaborInfo.LaborType='LAB' AND DamageLineInfo.LaborInfo.LaborInclInd=FALSE THEN DamageLineInfo.LaborInfo.LaborAmt
      WHEN DamageLineInfo.LaborInfo.LaborOperation='OP9'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP11'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP5'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP12'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP10'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP1'
    OR DamageLineInfo.LaborInfo.LaborOperation='OP21' THEN
    CASE
      WHEN DamageLineInfo.LaborInfo.LaborType='LAB' AND DamageLineInfo.LaborInfo.LaborInclInd=FALSE THEN DamageLineInfo.LaborInfo.LaborAmt
  END
  END as ReplaceTotal
  FROM
    UNNEST(ServiceBusEnvelope.Payload.RepairOrderFolderAddRq.DamageLineInfo) AS DamageLineInfo
  LIMIT 1
  )
WHERE
  TRUE