Google BigQuery 错误:UPDATE/MERGE 必须与每个目标行最多匹配一个源行
Posted
技术标签:
【中文标题】Google BigQuery 错误:UPDATE/MERGE 必须与每个目标行最多匹配一个源行【英文标题】:Google BigQuery Error: UPDATE/MERGE must match at most one source row for each target row 【发布时间】:2019-12-17 16:34:20 【问题描述】:我正在尝试通过比较(即更改的值)来更新引用新表的旧表。
我的查询如下:
UPDATE CogencyData.AUM
SET BeginBalanceBase = T1.BeginBalanceBase
FROM Cogency_Temp.AUM T1
LEFT OUTER JOIN CogencyData.AUM T2
ON T1.FundLongName = T2.FundLongName
AND T1.PeriodDate = T2.PeriodDate
WHERE COALESCE(T1.BeginBalanceBase, 0) <> COALESCE(T2.BeginBalanceBase);
但我收到以下错误:
UPDATE/MERGE must match at most one source row for each target row
我相信代码在 mysql 中运行良好。我该如何解决这个问题?
【问题讨论】:
注释掉前两行 (UPDATE
/SET
) 并替换为 SELECT T1.BeginBalanceBase as T1BBB, T2.BeginBalanceBase as T2BBB
。有结果吗?
@rtenha 是的。结果我得到了两个值 - 也许这就是查询被混淆的原因?如何有效地更新这两个值?
【参考方案1】:
这个查询会给你同样的错误:
CREATE OR REPLACE TABLE temp.users AS (
SELECT "1" id, 3 total
UNION ALL SELECT "2", 2 total
UNION ALL SELECT "3", 4 total
UNION ALL SELECT "4", 5 total
)
;
UPDATE temp.users T0
SET total = T1.total + T2.total
FROM temp.users T1
LEFT OUTER JOIN temp.users T2
ON T1.id = T2.id
WHERE 1>0
这个查询修复了错误:
CREATE OR REPLACE TABLE temp.users AS (
SELECT "1" id, 3 total
UNION ALL SELECT "2", 2 total
UNION ALL SELECT "3", 4 total
UNION ALL SELECT "4", 5 total
)
;
UPDATE temp.users T0
SET total = T1.total + T2.total
FROM temp.users T1
LEFT OUTER JOIN temp.users T2
ON T1.id = T2.id
WHERE 1>0
AND T0.id = T1.id
诀窍是什么:最后一个 WHERE
子句 T0.id = T1.id
确保正在更新的表中的每一行只获得一次更新。
【讨论】:
以上是关于Google BigQuery 错误:UPDATE/MERGE 必须与每个目标行最多匹配一个源行的主要内容,如果未能解决你的问题,请参考以下文章
错误:模块“google.cloud.bigquery_storage”没有属性“BigQueryReadClient”
在 Google BigQuery 中插入/更新 Firestore 文档导出