首页 > 解决方案 > 更新 SQL - 还购买了产品

问题描述

使用 BigQuery 食谱中的此查询,并添加到 _table_date_range_ 中,因为我想查询不止一天。

该查询查找产品并返回与该产品一起购买的其他产品。

SELECT hits.item.productName AS other_purchased_products, COUNT(hits.item.productName) AS quantity
FROM (TABLE_DATE_RANGE([ghd-analytics-xxxxxx.xxxxxx.ga_sessions_],
TIMESTAMP('2018-08-01'), TIMESTAMP('2018-09-31')))
WHERE fullVisitorId IN (
  SELECT fullVisitorId
  FROM (TABLE_DATE_RANGE([ghd-analytics-xxxxxx.xxxxxx.ga_sessions_],
TIMESTAMP('2018-08-01'), TIMESTAMP('2018-08-03')))
  WHERE hits.item.productName CONTAINS 'productA'
   AND totals.transactions>=1
  GROUP BY fullVisitorId )
 AND hits.item.productName IS NOT NULL
 AND hits.item.productName !='productA'
GROUP BY other_purchased_products
ORDER BY quantity DESC;

错误信息:

(L2:1):JOIN(包括半连接)和 UNION ALL(逗号,日期范围)不能组合在一个 SELECT 语句中。将 UNION ALL 移动到内部查询或将 JOIN 移动到外部查询。

我不知道该怎么做它的建议!

非常感谢您的帮助。

标签: sqlgoogle-analyticsgoogle-bigquery

解决方案


我不知道该怎么做它的建议!

您只是按照错误声明中的建议进行操作 -move the UNION ALL to an inner query
所以,在下面的固定版本中 - 我刚刚SELECT * FROM在第二行 添加

SELECT hits.item.productName AS other_purchased_products, COUNT(hits.item.productName) AS quantity
FROM (SELECT * FROM TABLE_DATE_RANGE([ghd-analytics-xxxxxx.xxxxxx.ga_sessions_],
TIMESTAMP('2018-08-01'), TIMESTAMP('2018-09-31')))
WHERE fullVisitorId IN (
  SELECT fullVisitorId
  FROM (TABLE_DATE_RANGE([ghd-analytics-xxxxxx.xxxxxx.ga_sessions_],
TIMESTAMP('2018-08-01'), TIMESTAMP('2018-08-03')))
  WHERE hits.item.productName CONTAINS 'productA'
   AND totals.transactions>=1
  GROUP BY fullVisitorId )
 AND hits.item.productName IS NOT NULL
 AND hits.item.productName !='productA'
GROUP BY other_purchased_products
ORDER BY quantity DESC     

注意:我希望你有充分的理由在这里使用 Legacy SQL - 无论如何考虑迁移到标准 SQL


推荐阅读