Google Bigquery - 查询无效

Posted

技术标签:

【中文标题】Google Bigquery - 查询无效【英文标题】:Google Bigquery - Invalid query 【发布时间】:2018-12-05 19:16:58 【问题描述】:

我在 Google BigQuery(见下文)中进行了以下查询(标准 SQL),但它导致了错误。有谁知道出了什么问题?在查询中,出于保密原因,我屏蔽了 project-id 和 dataset-id。

有谁知道如何解决该错误并获得正确的查询?提前非常感谢!

所需尺寸:

Date
hits.product.productSKU
custom dimension 47 (= product scope dimension)
custom dimension 48 (= product scope dimension)
hits.eCommerceAction.action_type
hits.eCommerceAction.step

所需指标:

COUNT(hits.eCommerceAction.action_type)
COUNT(hits.product.productSKU)

下面的查询出现如下错误:

"Cannot access field productSKU on a value with type ARRAY<STRUCT<productSKU STRING, v2ProductName STRING, v2ProductCategory STRING, ...>> at [3:16]"

查询:

SELECT
date AS Date,
hits.product.productSKU AS SKU,
(
SELECT
cd.value
FROM
hits.customDimensions AS cd
WHERE
cd.index=47 ) AS CD47,
(
SELECT
cd.value
FROM
hits.customDimensions AS cd
WHERE
cd.index=48 ) AS CD48,
hits.eCommerceAction.action_type AS Type,
hits.eCommerceAction.step AS Step,
COUNT(hits.eCommerceAction.action_type) AS Nr,
COUNT(hits.product.productSKU) AS NrSKU
FROM
`[projectid].[datasetid].ga_sessions*`,
UNNEST(hits) AS hits
WHERE
_TABLE_SUFFIX BETWEEN '20181103'
AND '20181103'
AND hits.page.hostname = 'www.bla.nl'
GROUP BY
Date,
Step,
Type,
SKU,
CD47,
CD48

【问题讨论】:

您是否要计算product 数组中的条目数?也许你想要SUM(ARRAY_LENGTH(hits.product)) 您的hits.product 字段似乎是一个结构数组。你能确认一下吗?如果是这样,您将不得不取消嵌套它。 @Teddy,已确认。取消嵌套工作。 @ElliottBrossard,谢谢。使用 SUM(ARRAY_LENGTH(hits.product)) 有什么好处? 【参考方案1】:

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

以下 [仅] 解决上述问题并假设其余逻辑是正确的,并且应该返回任何 OP 期望返回的内容

#standardSQL
SELECT
  DATE AS DATE,
  prod.productSKU AS SKU,
  ( SELECT cd.value
    FROM hits.customDimensions AS cd
    WHERE cd.index=47) AS CD47,
  ( SELECT cd.value
    FROM hits.customDimensions AS cd
    WHERE cd.index=48 ) AS CD48,
  hits.eCommerceAction.action_type AS Type,
  hits.eCommerceAction.step AS Step,
  COUNT(hits.eCommerceAction.action_type) AS Nr,
  COUNT(prod.productSKU) AS NrSKU
FROM `[projectid].[datasetid].ga_sessions*`,
UNNEST(hits) AS hits, UNNEST(hits.product) prod
WHERE _TABLE_SUFFIX BETWEEN '20181103' AND '20181103'
AND hits.page.hostname = 'www.bla.nl'
GROUP BY DATE, Step, Type, SKU, CD47, CD48   

正如您在此处看到的 - 我添加了 UNNEST(hits.product) prod 并将对 hits.product.productSKU 的引用替换为 prod.productSKU

【讨论】:

非常感谢米哈伊尔,这行得通!只有一个问题:2 个产品自定义维度为所有行提供值“null”。不幸的是,将“FROM hits.customDimensions AS cd”更改为“FROM hits.product.customDimensions AS cd”不起作用。您还知道解决此问题的方法吗? 对我之前评论的反馈:我已经通过将“hits.customDimensions”更改为“prod.customDimensions”来修复它。这行得通。【参考方案2】:

您必须取消嵌套product。以下是使用公共 ga 数据的示例:

select date, productSKU, CD47, CD48, Type, Step, avg(array_length(nr)), avg(array_length(skus))  from (
  SELECT
    date AS Date,
    array((select p.productSKU as productSKU from unnest(hits.product) p)) skus,
    (SELECT cd.value FROM hits.customDimensions AS cd WHERE cd.index=47) AS CD47,
    (SELECT cd.value FROM hits.customDimensions AS cd WHERE cd.index=48) AS CD48,
    hits.eCommerceAction.action_type AS Type,
    hits.eCommerceAction.step AS Step,
    array((select hits.eCommerceAction.action_type)) AS Nr
  from 
  `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`, unnest(hits) hits
), unnest(skus) productSKU
group by 
date, 
productSKU, 
CD47, 
CD48, 
Type, 
Step

【讨论】:

以上是关于Google Bigquery - 查询无效的主要内容,如果未能解决你的问题,请参考以下文章

Google Data Studio:如何使用自定义 Big Query 查询创建时间序列图表

使用 Google Apps 脚本将查询中的数据加载到 Big Query - 缺少必需参数

Big Query 着陆页数字与 Google Analytics 界面不一致

将Big Query中的数据自动导入Google表格?

如何优化 Google Big Query 计费字节数

使用 Python Pycharm 在 Big Query 中查询公共数据集