在不丢失 BigQuery 中的导出模式的情况下计算嵌套字段

Calculate nested field without loosing export schema in BigQuery

字段计算导致 BigQuery 中的原始导出架构丢失。

我有一个标准的增强型电子商务模式,想将 transactionRevenue 更改为不同的货币。我想保留一般的导出模式结构。计算字段 "transactionRevenueNewCurrency" 应该在 hits.transaction.transactionRevenueNewCurrency.

#standardSQL
SELECT
s.*,
ARRAY(SELECT  COALESCE( x.transaction.transactionRevenue*1.17,0)
        FROM UNNEST(hits) AS x) AS transactionRevenueNewCurrency
FROM `bigquery-public-data.google_analytics_sample.ga_sessions_*` as s , UNNEST(hits) as h
WHERE
_TABLE_SUFFIX BETWEEN '20160801' AND '20160831'
AND transaction.transactionRevenue >0
LIMIT 10000

新字段附加到会话而不是每次点击。

以下适用于 BigQuery 标准 SQL

#standardSQL
SELECT * REPLACE(
  ARRAY(
    SELECT AS STRUCT * REPLACE(
      (SELECT AS STRUCT * REPLACE(
        COALESCE(CAST(transactionRevenue * 1.17 AS INT64), 0
        ) AS transactionRevenue)
       FROM UNNEST([transaction])
      ) AS transaction)
    FROM UNNEST(hits) hit
  ) AS hits)
FROM `bigquery-public-data.google_analytics_sample.ga_sessions_*` 
WHERE _TABLE_SUFFIX BETWEEN '20160801' AND '20160831'