更新SQL-还购买了产品

时间:2019-04-15 15:37:33

标签: sql google-analytics google-bigquery

使用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移动到外部查询。

我不确定该怎么做!

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

  

我不确定如何执行建议的操作!

您只是按字面意思执行错误声明中建议的操作-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     

注意:我希望您有充分的理由在这里使用旧版SQL-无论如何都要考虑使用Migrating to Standard SQL