Unnest hits.product.customDimensions错误

时间:2018-01-30 16:00:24

标签: sql google-analytics google-bigquery

我在查询hits.product.customDimensions时遇到问题(hits.customDimensions上的逻辑相同)。我无法理解为什么额外的嵌套会导致数组错误。任何帮助赞赏。谢谢!

标准SQL

SELECT
  fullVisitorId,
  visitId,
  hits.hitNumber,
  product.productSKU,
  MAX(IF(c.index=1,c.value, null)) AS customDimesion1
FROM 17823880.ga_sessions_20180128,
UNNEST(hits) AS hits, 
UNNEST(hits.product) as product,
UNNEST(hits.product.customDimensions) as c
GROUP BY 1, 2, 3, 4

错误

  

无法对具有类型的值访问字段customDimensions   ARRAY>在[11:23]

标准SQL - 此查询无效,

SELECT
  fullVisitorId,
  visitId,
  hits.hitNumber,
  MAX(IF(c.index=1,c.value, null)) AS customDimesion1
FROM 17823880.ga_sessions_20180128,
UNNEST(hits) AS hits,
UNNEST(hits.customDimensions) as c
GROUP BY 1, 2, 3

1 个答案:

答案 0 :(得分:2)

问题是您为来自product的元素提供了UNNEST(hits.product)的别名,但您未在后续UNNEST(hits.product.customDimensions)中引用该别名,因此在取消之后,你最终得到了原始的product数组而不是它的元素。试试这个:

SELECT
fullVisitorId,
visitId,
hits.hitNumber,
product.productSKU,
MAX(IF(c.index=1,c.value, null)) AS customDimesion1
FROM 17823880.ga_sessions_20180128,
UNNEST(hits) AS hits, 
UNNEST(hits.product) as product,
UNNEST(product.customDimensions) as c
GROUP BY 1, 2, 3, 4