无法访问类型为ARRAY <struct <productsku string,=“” v2productname =“ =” string,=“” v2productcategory =“” string,=“” ... =“” >>的值的字段productQuantity

时间:2019-05-09 17:23:43

标签: google-analytics google-bigquery standard-sql

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

  

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

我用Google搜索了它,并且已经按照其他答案中的建议使用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

1 个答案:

答案 0 :(得分:1)

  

无法访问类型为ARRAY>的值的字段productQuantity

您应使用以下方法

#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   
  

有没有一种方法可以在不重复代码的情况下从特定时间范围进行查询?

是的,您可以为此使用_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'