无法访问类型为 ARRAY<STRUCT<productSKU STRING, v2ProductName STRING, v2ProductCategory STRING, ...>> 的值的字段 productQuantity

Cannot access field productQuantity on a value with type ARRAY<STRUCT<productSKU STRING, v2ProductName STRING, v2ProductCategory STRING, ...>>

我正在尝试使用 BigQuery 从 Google Analytics 查询数据。在我 运行 之前它给了我这个错误:

Cannot access field productQuantity on a value with type ARRAY<STRUCT<productSKU STRING, v2ProductName STRING, v2ProductCategory STRING, ...>>

我用谷歌搜索了一下,我已经按照其他一些答案中的建议使用了 UNNEST 函数。我不确定出了什么问题。

此外,我正在从 Google Analytics 中的不同表进行查询,并且数据按日期存储。有没有一种方法可以在不重复代码的情况下从特定时间范围内查询?

请看下面我的代码:

#standardSQL
SELECT
  date,
  hits.transaction.transactionId,
  hits.product.productQuantity
FROM
  `XXX1`,
  UNNEST(hits) AS hits,
  UNNEST(hits.product.productQuantity) AS prod
GROUP BY
  date
UNION ALL
SELECT
  date,
  hits.transaction.transactionId,
  hits.product.productQuantity
FROM
  `XXX2` UNNEST(hits) AS hits,
  UNNEST(hits.product.productQuantity) AS prod
GROUP BY
  date
UNION ALL
SELECT
  date,
  hits.transaction.transactionId,
  hits.product.productQuantity
FROM
  `XXX3` UNNEST(hits) AS hits,
  UNNEST(hits.product.productQuantity) AS prod
GROUP BY
  date
UNION ALL
SELECT
  date,
  hits.transaction.transactionId,
  hits.product.productQuantity
FROM
  `XXX4` UNNEST(hits) AS hits,
  UNNEST(hits.product.productQuantity) AS prod
GROUP BY
  date
UNION ALL
SELECT
  date,
  hits.transaction.transactionId,
  hits.product.productQuantity
FROM
  `XXX5` UNNEST(hits) AS hits,
  UNNEST(hits.product.productQuantity) AS prod
GROUP BY
  date
UNION ALL
SELECT
  date,
  hits.transaction.transactionId,
  hits.product.productQuantity
FROM
  `XXX6` UNNEST(hits) AS hits,
  UNNEST(hits.product.productQuantity) AS prod
GROUP BY
  date

Cannot access field productQuantity on a value with type ARRAY>

您应该使用以下方法

#standardSQL
SELECT
  date,
  hits.transaction.transactionId, 
  prod.productQuantity
FROM `XXX`,
UNNEST(hits) AS hits,
UNNEST(hits.product) AS prod

因此,如您所见,productQuantity 是使用未嵌套 'prod'

访问的

注意:当您使用 GROUP BY 时,您需要对 select 语句中不属于 GROUP BY 的那些字段使用聚合函数 - 在您的示例中,下面有两个字段需要适用于您正在寻找的任何聚合,以防您仍然需要 GROUP BY

hits.transaction.transactionId, 
prod.productQuantity   

Is there a way that I can query from a specific time frame without repeating the code?

是的,您可以为此使用 _TABLE_SUFFIX

就像下面的例子

#standardSQL
SELECT
  date,
  hits.transaction.transactionId, 
  prod.productQuantity
FROM `project.dataset.XXX*`,
UNNEST(hits) AS hits,
UNNEST(hits.product) AS prod   
WHERE _TABLE_SUFFIX BETWEEN '1' AND '6'