我正在尝试使BigQuery中具有的当前工作的SQL查询更加简化,并遇到以下问题:
错误:ON子句必须为AND of =每个表中一个字段名称的比较,所有字段名称都以表名称为前缀。考虑使用标准SQL(.google.com / bigquery / docs / reference / standard-sql /),该方法允许进行包含表达式和残差谓词的非等分JOIN和比较。
下面是给出上面错误的查询。第一个LEFT JOIN起作用。当我在下面添加第二个时,我开始收到错误消息。我想做的是获取人类可读的own.o.firstname和own.o.lastname值,而不是交易记录的owner_id值(o.properties.hubspot_owner_id.value),但为了做到这一点,我需要加入一些表。
我必须在第二个JOIN的ON子句上使用CAST,因为字段在每个表的各自模式中具有不同的类型。如果我不这样做,则会出现以下错误:错误:连接键o.properties.hubspot_owner_id.value(字符串)和o.ownerid(int64)具有无法自动强制的类型。
WHERE子句只是一个禁止列表,不返回已从数据库中删除的条目。
SELECT o.*
FROM (
SELECT
o.dealid,
o.properties.dealname.value,
stages.Label,
o.properties.closedate.value,
o.properties.hubspot_owner_id.value,
own.o.firstname,
own.o.lastname,
o.properties.amount.value,
o.properties.createdate.value,
o.properties.pipeline.value,
o.associations.associatedcompanyids,
ROW_NUMBER() OVER (PARTITION BY o.dealid ORDER BY o._sdc_batched_at DESC) as seqnum
FROM [sample-table:hubspot.deals] o
LEFT JOIN [sample-table:hubspot.sales_stages_lookup] stages ON o.properties.dealstage.value = stages.Internal_Value
LEFT JOIN [sample-table:hubspot.owners_reporting] own ON CAST(o.properties.hubspot_owner_id.value AS INTEGER) = CAST(own.o.ownerid AS INTEGER)) o
WHERE o.dealid NOT IN (SELECT objectid FROM [sample-table:hubspot_suppression_list.data] WHERE subscriptiontype = 'deal.deletion') AND seqnum = 1
答案 0 :(得分:0)
在BigQuery中使用standard SQL,它支持将表达式作为ON
子句的一部分:
#standardSQL
SELECT o.*
FROM (
SELECT
o.dealid,
o.properties.dealname.value AS dealname_value,
stages.Label,
o.properties.closedate.value AS closedate_value,
o.properties.hubspot_owner_id.value AS hubspot_owner_id_value,
own.o.firstname,
own.o.lastname,
o.properties.amount.value AS amount_value,
o.properties.createdate.value AS createdate_value,
o.properties.pipeline.value AS pipeline_value,
o.associations.associatedcompanyids,
ROW_NUMBER() OVER (PARTITION BY o.dealid ORDER BY o._sdc_batched_at DESC) as seqnum
FROM `sample-table.hubspot.deals` o
LEFT JOIN `sample-table.hubspot.sales_stages_lookup` stages ON o.properties.dealstage.value = stages.Internal_Value
LEFT JOIN `sample-table.hubspot.owners_reporting` own ON CAST(o.properties.hubspot_owner_id.value AS INT64) = CAST(own.o.ownerid AS INT64)) o
WHERE o.dealid NOT IN (SELECT objectid FROM `sample-table.hubspot_suppression_list.data` WHERE subscriptiontype = 'deal.deletion') AND seqnum = 1
有关BigQuery中旧版SQL与标准SQL之间的区别的更多信息,请参见migration guide。