Google Bigquery-无效的查询

时间:2018-12-05 19:16:58

标签: google-bigquery

我在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

2 个答案:

答案 0 :(得分:0)

  

...无法访问类型为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的引用

答案 1 :(得分:0)

您必须取消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