不在GROUP BY和HAVING中使用

时间:2014-11-10 13:48:16

标签: 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,但显然他们无法提供帮助。

2 个答案:

答案 0 :(得分:3)

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

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

在您的查询中

没有" Order_Id"在select子句中。 尝试将它放在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

替换

答案 1 :(得分:1)

作为BigQuery团队的成员,我能够运行您的查询并尝试不同的方法。只要删除对o.order_id的引用并修改某些字段名称,它似乎可以使用反连接(NOT IN)运行。

SELECT
  campaign.id AS campaign_id,
  campaign.template_id AS campaign_template_id,
  campaign.lang AS language,
  campaign.split_idx AS campaign_split_id,
  COUNT(DISTINCT order_id) AS orders,
  SUM(total) AS totalrevenue,
  SUM(shipping_value) AS shipping_value
FROM
  [wr_live.order]
WHERE
  order_id NOT IN (
  SELECT
    order_id
  FROM
    [wr_live.order_event]
  WHERE
    event = 'payment'
    AND status = 1
    )
GROUP BY
  campaign_id,
  campaign_template_id,
  language,
  campaign_split_id;

此查询在不到10秒的时间内运行了几次。我无法保证它总是那么快,但你可能想尝试一下。