【问题标题】:Keep getting "Cannot access field item on a value with type ARRAY<STRUCT<hitNumber INT64, time INT64, hour INT64, ...>>"不断收到“无法访问类型为 ARRAY<STRUCT<hitNumber INT64, time INT64, hour INT64, ...>> 的值的字段项”
【发布时间】:2021-03-11 07:26:05
【问题描述】:

我正在复制并粘贴 Google Analytics Cookbook 中的代码:

SELECT one.hits.item.productSku AS ProductSku, ( sum_of_hit_number / total_hits ) AS avg_hit_number
FROM (
 SELECT hits.item.productSku, SUM(hits.hitNumber) AS sum_of_hit_number
 FROM [‘GA Dataset Name’ ]
 WHERE hits.item.productSku IS NOT NULL
  AND totals.transactions>=1
 GROUP BY hits.item.productSku ) AS ‘Alias_Name_1’
JOIN (
 SELECT hits.item.productSku, COUNT( fullVisitorId ) AS total_hits
 FROM [‘GA Dataset Name’ ]
 WHERE hits.item.productSku IS NOT NULL
  AND totals.transactions>=1
 GROUP BY hits.item.productSku ) AS ‘Alias_Name_2’
ON Alias_Name_1.hits.item.productSku = Alias_Name_2.hits.item.productSku;

但我将其编辑为:

SELECT one.hits.item.productSku AS ProductSku, ( sum_of_hit_number / total_hits ) AS avg_hit_number
FROM (
 SELECT hits.item.productSku, SUM(hits.hitNumber) AS sum_of_hit_number
 FROM `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`
 WHERE hits.item.productSku IS NOT NULL
  AND totals.transactions>=1
 GROUP BY hits.item.productSku ) AS ProductSKU_Item
JOIN (
 SELECT hits.item.productSku, COUNT( fullVisitorId ) AS total_hits
 FROM `bigquery-public-data.google_analytics_sample.ga_sessions_20170801`
 WHERE hits.item.productSku IS NOT NULL
  AND totals.transactions>=1
 GROUP BY hits.item.productSku ) AS product2_item
ON ProductSKU_Item.hits.item.productSku = product2_item.hits.item.productSku;

我仍然收到一条错误消息,提示 Cannot access field item on a value on type ARRAY> at [5:13] 但是当我尝试 UNNEST 功能时,它仍然无法正常工作,我不确定自己做错了什么。

【问题讨论】:

    标签: sql standards


    【解决方案1】:

    我使用unnest 犯的错误是尝试取消嵌套hits.item,即struct。 hits 是需要unnested 的数组。试试这个:

    SELECT
      hits.item.productSku,
      SUM(hits.hitNumber) / COUNT( fullVisitorId ) AS avg_hit_number
    FROM
      `bigquery-public-data.google_analytics_sample.ga_sessions_20170801` t
    CROSS JOIN
      UNNEST(t.hits) AS hits
    WHERE
      hits.item.productSku IS NOT NULL
      AND totals.transactions >= 1
    GROUP BY
      hits.item.productSku
    

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 2019-07-27
      • 2018-11-26
      • 1970-01-01
      • 2019-02-12
      • 2022-01-24
      • 2021-09-21
      • 1970-01-01
      • 2014-02-04
      相关资源
      最近更新 更多