google-bigquery - 不与 GROUP BY 和 HAVING 一起使用

标签 google-bigquery

我们正在尝试对所有订单收入进行求和,但我们没有注册付款成功事件。 (例如:失败的订单)失败的订单要么没有尝试支付,要么支付失败。

SELECT
o.campaign.id AS campaign_id,
o.campaign.template_id AS campaign_template_id,
o.campaign.lang AS language,
o.campaign.split_idx AS campaign_split_id,
count(distinct o.order_id) AS orders,
SUM(o.total) AS totalrevenue,
SUM(o.shipping_value) AS shipping_value
FROM [wr_live.order] AS o
GROUP BY campaign_id, campaign_template_id, language, campaign_split_id
HAVING
o.order_id NOT IN (
SELECT order_id
FROM [wr_live.order_event] as e
WHERE e.order_id = o.order_id AND e.event = 'payment' and e.status = 1
) 

.

Query Failed
Error: Field 'o.order_id' not found in table 'e'; did you mean 'order_id'?
Job ID: aerobic-forge-504:job_h41lud83lyqiD7p6qldXMl_tx0A

如您所见,我们正在运行一个 NOT IN 子句,我们会检查该特定订单是否没有任何付款事件,并且状态 =1。

我们有关于付费 Google 企业支持的案例 05024161,但显然他们无法提供帮助。

最佳答案

“请注意,HAVING 子句只能引用 SELECT 子句中定义的字段(如果该字段有别名,则必须使用它;如果没有,则使用聚合字段名称)。”

https://cloud.google.com/bigquery/query-reference#having

在您的查询中,select 子句中没有“Order_Id”。 尝试将其放在 group by 之前的 where 子句中。

类似于:

SELECT
o.campaign.id AS campaign_id,
o.campaign.template_id AS campaign_template_id,
o.campaign.lang AS language,
o.campaign.split_idx AS campaign_split_id,
count(distinct o.order_id) AS orders,
SUM(o.total) AS totalrevenue,
SUM(o.shipping_value) AS shipping_value
FROM(
Select * from  [wr_live.order] 
Where order_id NOT IN (
SELECT order_id
FROM [wr_live.order_event] as e
WHERE  e.event = 'payment' and e.status = 1
) 
)AS o
GROUP BY campaign_id, campaign_template_id, language, campaign_split_id

此外BQ不支持子查询引用父查询。如果需要请尝试替换为Join

关于google-bigquery - 不与 GROUP BY 和 HAVING 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26845334/

相关文章:

c# - 流式传输数据时出现 Bigquery internalError

sql - BigQuery JOIN 错误

google-bigquery - 在 Google BigQuery 中选择除某些列之外的所有列?

sql - 如何在 Google Big Query 中实现 MINUS 运算符

google-analytics - 通用分析BigQuery导出:从_ga Cookie中检索fullVisitorId

google-bigquery - 如何在 BigQuery 中的 SELECT * 语句中将字符串附加/前置/连接到所有字段名称

pandas - 从 BigQuery 加载大量数据到 python/pandas/dask

google-cloud-platform - Google Analytics -> Bigquery -> 每日导出到 IBM 云存储

go - 使用没有协议(protocol)定义的托管编写器

google-bigquery - 更新bigquery表的不同方式