【发布时间】:2023-03-12 03:01:01
【问题描述】:
我正在尝试在 BigQuery 中合并 2 个分区表:
-
'source_t' 是一个源表。它通过 Ingestion Time 和 Partition filter 进行分区 –
Required. Pseudo field _PARTITIONTIME is timestamp -
'target_t' 是一个使用分区过滤器按字段“日期”分区的目标表
Required. Field date is date
我想从 源 表的最后一个分区获取数据并将其合并到 目标 表。要过滤 tagret 表上的搜索任务,我需要使用 source 表数据中的“日期”字段。我写了一个查询,但编辑器显示以下查询错误:
如果不过滤列“日期”,则无法查询表“MyDataSet.target_t”
这是我的查询:
declare latest default (select date(max(_PARTITIONTIME)) as latest from MyDataSet.source_t where _PARTITIONTIME >= timestamp(date_sub(current_date(),interval 1 day)));
declare first_date default (select min(date) as first_date from MyDataSet.source_t where date(_PARTITIONTIME) = latest);
merge `MyDataSet.target_t` as a
using (select * from `MyDataSet.source_t` where _PARTITIONTIME = latest) as b
on
a.date >= first_date and
a.date = b.date and
a.account_id = b.account_id and
a.campaign_id = b.campaign_id and
a.adset_id = b.adset_id and
a.ad_id = b.ad_id
when matched then update set
a.account_name = b.account_name,
a.campaign_name = b.campaign_name,
a.adset_name = b.adset_name,
a.ad_name = b.ad_name,
a.impressions = b.impressions,
a.clicks = b.clicks,
a.spend = b.spend,
a.date = b.date
when not matched then insert row;
如果我输入日期而不是“最新”变量(“where _PARTITIONTIME = '2020-10-01') as b”),则不会出现任何错误。但我想正确过滤源表。
而且我不明白它如何影响下面的“on”声明以及为什么一切都坏了>。
【问题讨论】:
标签: google-bigquery