BigQuery使用新的SQL语法访问CustomDimensions

时间:2017-03-22 13:56:19

标签: sql google-analytics google-bigquery

我正在迁移到BigQuery中的新SQL语法,因为它似乎更灵活。但是,在访问customDimensions中的字段时,我有点卡住了。我写的东西很简单:

SELECT 
  cd.customDimensions.index,
  cd.customDimensions.value
FROM `xxxxx.ga_sessions_20170312`, unnest(hits) cd
limit 100

但是我收到了错误

Error: Cannot access field index on a value with type ARRAY<STRUCT<index INT64, value STRING>>

然而,如果我运行这样的东西完全正常:

    SELECT 
        date,
        SUM((SELECT SUM(latencyTracking.pageLoadTime) FROM UNNEST(hits))) pageLoadTime,
        SUM((SELECT SUM(latencyTracking.serverResponseTime) FROM UNNEST(hits))) serverResponseTime
   FROM `xxxxxx.ga_sessions_20170312`
    group by 1

在查询customDimensions时是否有一些不同的逻辑?

2 个答案:

答案 0 :(得分:4)

如果打算以展平形式检索所有自定义维度,请同时加入UNNEST(customDimensions)

#standardSQL
SELECT 
  cd.index,
  cd.value
FROM `xxxxx.ga_sessions_20170312`,
  unnest(hits) hit,
  unnest(hit.customDimensions) cd
limit 100;

答案 1 :(得分:1)

SELECT
fullvisitorid,
( SELECT MAX(IF(index=1,value, NULL))FROM UNNEST(hits.customDimensions)) AS CustomDimension1,
( SELECT MAX(IF(index=2,value, NULL))FROM UNNEST(hits.customDimensions)) AS CustomDimension2
FROM
  `XXXXXXX`, unnest(hits) as hits