简体   繁体   English

BigQuery:查询错误:MERGE 表时列名_PARTITIONDATE 不明确

[英]BigQuery : Query error: Column name _PARTITIONDATE is ambiguous when MERGE table

I have a query that will merge the source temp table to target table when there's new row in source that not in target table.我有一个查询,当源中有新行而不在目标表中时,它会将源临时表合并到目标表。 the source table used a WITH clause and ROW_NUMBER() to get a non-duplicate row from temp table.源表使用 WITH 子句和 ROW_NUMBER() 从临时表中获取非重复行。 I used an ingestion-time partitioned table (day) which mean that during merge I have to specify the column name as from the official doc (Both table have the same table schema)我使用了一个摄取时间分区表(天),这意味着在合并期间我必须指定来自官方文档的列名(两个表具有相同的表架构)

MERGE mydataset.myfinaltable T 
USING (
 WITH myNonDupSource AS (
        SELECT
        *,
        ROW_NUMBER() OVER (PARTITION BY product, barcode) AS RowNumber
        FROM mySourceTempTable
    )
    SELECT * EXCEPT (RowNumber)
    FROM myNonDupSource
    WHERE RowNumber = 1
) as S
ON
    T.product = S.product
    and  T.barcode= S.barcode
WHEN NOT MATCHED THEN
INSERT (
    col_a,
    col_b,
    col_c,
    product,
    barcode
)
VALUES (
    S.col_a,
    S.col_b,
    S.col_c,
    S.product,
    S.barcode
);

I ran the query and it show query error: Column name _PARTITIONDATE is ambiguous at mydataset.myfinaltable .我运行查询并显示query error: Column name _PARTITIONDATE is ambiguous at mydataset.myfinaltable

At first I try INSERT (col_a,col_b, col_c,product, barcode) ROW;起初我尝试INSERT (col_a,col_b, col_c,product, barcode) ROW; But it produced the same error.但它产生了同样的错误。 Not sure which part that I missed, it shouldn't be ambiguous because I specified the column to be inserted but why the _PARTITIONDATE column still thrown an error不确定我错过了哪一部分,它不应该是模棱两可的,因为我指定了要插入的列但是为什么 _PARTITIONDATE 列仍然抛出错误

When you are joining multiple tables in a SQL query and if a column with the same name is present in both of the tables, Bigquery does not know which one to use (unless you explicitly tell so), so it throws the ambiguous column name error.当您在 SQL 查询中连接多个表时,如果两个表中都存在同名的列,Bigquery 不知道使用哪一个(除非您明确告知),因此它会抛出不明确的列名错误.

Bigquery WebUI editor is intelligent enough to highlight the exact row in which the ambiguous column is present (look for the red exclamation on the margin of the sql editor). Bigquery WebUI 编辑器足够智能,可以突出显示不明确的列所在的确切行(查找 sql 编辑器边距上的红色感叹号)。 You can see more documentation .您可以查看更多文档

You need to add the name of the fields instead all the fields (*) like these examples:您需要添加字段名称而不是所有字段 (*),如以下示例:

MERGE mydataset.myfinaltable T 
USING (
 WITH myNonDupSource AS (
        SELECT
        fieldname1,fieldname2,fieldname3,
        ROW_NUMBER() OVER (PARTITION BY product, barcode) AS RowNumber
        FROM mySourceTempTable
    )
    SELECT fieldname1,fieldname2,fieldname3 EXCEPT (RowNumber)
    FROM myNonDupSource
    WHERE RowNumber = 1
) as S
ON
    T.product = S.product
    and  T.barcode= S.barcode
WHEN NOT MATCHED THEN
INSERT (
    col_a,
    col_b,
    col_c,
    product,
    barcode
)
VALUES (
    S.col_a,
    S.col_b,
    S.col_c,
    S.product,
    S.barcode
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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