无法使用 google bigquery(标准)取消嵌套某些字段

Unable to un-nest some fields using google bigquery (standard)

我有一个嵌套的 table,我无法访问使用标准 google bigquery 的所有字段。

例如这个查询失败

SELECT  *
FROM 
    (
    SELECT
           rev_info.user.id as player_id,
           rev_info.purchase.total.currency as currency,
           rev_info.purchase.total.amount as REV
          ,rev_info.purchase.virtual_items.items.sku     as sku 
    FROM `gcs.rev`
    )
WHERE currency = 'USD'

有错误

"Error: Cannot access field sku on a value with type ARRAY> at [9:59]"

然而

SELECT  *
FROM 
    (
    SELECT
           rev_info.user.id as player_id,
           rev_info.purchase.total.currency as currency,
           rev_info.purchase.total.amount as REV
          --,rev_info.purchase.virtual_items.items.sku   as sku 
    FROM `gcs.rev`
    )
WHERE currency = 'USD'

这个查询没问题。

另请注意

SELECT
       rev_info.purchase.virtual_items.items.sku     as sku 
FROM `gcs.rev`

失败并出现与上述相同的错误。

如果您的目标是为每个 items 数组元素获取一行,那么您可以在 table 和 rev_info.purchase.virtual_items.items 之间使用逗号(连接)运算符。例如,

SELECT *
FROM (
  SELECT
    rev_info.user.id as player_id,
    rev_info.purchase.total.currency as currency,
    rev_info.purchase.total.amount as REV,
    item.sku as sku 
  FROM `gcs.rev` t,
    t.rev_info.purchase.virtual_items.items item
)
WHERE currency = 'USD'

扩展 Elliott 的回答 - 我认为在这里您首先需要 UNNEST,但随后您很可能需要聚合回您的 sku。否则你会得到非常冗余(扁平化)的输出

我觉得下面是您可能需要的 - 它适用于 BigQuery Standard SQL

#standardSQL
SELECT 
  player_id, 
  currency, 
  REV, 
  STRING_AGG(sku) SKUs
FROM (
  SELECT
    rev_info.user.id AS player_id,
    rev_info.purchase.total.currency AS currency,
    rev_info.purchase.total.amount AS REV,
    item.sku AS sku 
  FROM `gcs.rev` t,
  UNNEST(t.rev_info.purchase.virtual_items.items) item
)
WHERE currency = 'USD'
GROUP BY 1, 2, 3   

因此,所有 sku 将显示为给定 player_id 的列表,以及金额和货币

已添加,根据 Elliott 的 comment/suggestion

#standardSQL
SELECT
  rev_info.user.id AS player_id,
  rev_info.purchase.total.currency AS currency,
  rev_info.purchase.total.amount AS REV,
  (SELECT STRING_AGG(item.sku) 
     FROM UNNEST(t.rev_info.purchase.virtual_items.items) item
  ) AS SKUs 
FROM `gcs.rev` t,
WHERE currency = 'USD'