gpt4 book ai didi

google-bigquery - 合并语句的 BigQuery 等价物

转载 作者:行者123 更新时间:2023-12-05 02:52:50 25 4
gpt4 key购买 nike

我正在执行从 Teradata 到大查询的迁移。我遇到了在 USING 子句中包含 VALUES 的合并语句。

MERGE INTO department DL
USING VALUES
(
2,'ABC'
) AS V
(Run_Id, Country)
ON DL.department_id = V.Run_Id
WHEN MATCHED THEN
UPDATE SET
department_description = V.country
WHEN NOT MATCHED THEN
INSERT
(
V.Run_Id
, V.Country
curr
);

任何人都可以帮助我找到它的 BigQuery 等价物。

最佳答案

MERGE 语句用于更新目标表,使用源表或多个主键(PK)。

根据documentation ,Teradata 和 BigQuery 的 MERGE 之间的区别是:

Teradata's MERGE operation is limited to matching primary keys withinone access module processor (AMP). In contrast, BigQuery has no sizeor column limitation for MERGE operations, therefore using MERGE is auseful optimization. However, if the MERGE is primarily a largedelete, see optimizations for DELETE elsewhere in this document.

DML scripts in BigQuery have slightly different consistency semanticsthan equivalent statements in Teradata. For example, Teradata's SETtables in session mode might ignore duplicates during a MERGEoperation. For an overview on handling MULTISET and SET tables,snapshot isolation, and session and transaction handling, see theCREATE INDEX section elsewhere in this document.

在您的情况下,您似乎正在使用 PK 作为 DL.department_idV.Run_Id。虽然,在 USING 子句内的语法中,您应该指定目标表,而不仅仅是它的字段。下面是语法,link :

MERGE target_name [[AS] alias]
USING source_name
ON merge_condition
#WHEN MATCHED
#WHEN NOT MATCHED

因此,在您的情况下,语法将是:

MERGE dataset.department DL
USING (SELECT * FROM `project_id.dataset.source_table`) V
ON DL.department_id = V.Run_Id
WHEN MATCHED THEN
UPDATE SET DL.department_description = V.country
WHEN NOT MATCHED
#first specify the name of the columns in your then the values to insert
INSERT(colum1, column2, column3) VALUES(V.Run_Id, V.Country, V.curr)

请注意,在 INSERT 子句中,您首先指定要添加数据的列,然后在 VALUES 中指定要插入的值,您可以显式写入值或命名source_table 中包含要添加的数据的列。我想指出的是,我将 curr 视为源表中的一列。另外,您没有说明您的源表,只说明了它的一些字段。

为了进一步说明,下面是另一个例子

MERGE `dataset.target_table` T
USING (SELECT "New value" as value, "1" as ID) S
ON T.ID = S.ID
WHEN MATCHED THEN
UPDATE SET T.value_column = S.value
WHEN NOT MATCHED THEN
INSERT(value_column, id) VALUES("Value added", s.ID)

再次注意 INSERT 子句,首先描述目标表中的列,然后是将插入到表中的值 WHEN NOT MATCHED

关于google-bigquery - 合并语句的 BigQuery 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62484110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com