【问题标题】:Transpose nested rows into columns in bigquery with google analytics data使用谷歌分析数据将嵌套行转换为 bigquery 中的列
【发布时间】:2015-06-22 03:04:25
【问题描述】:

我有兴趣使用自定义维度属性来吸引访问者,其中每一行都是唯一的 fullvisitorid,列是所需的 customdimension.values。

以伦敦头盔为例,在这里我用我感兴趣的两个自定义维度来吸引访问者:

SELECT fullvisitorid, customDimensions.index, customDimensions.value
FROM [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
where customDimensions.index in (2,3)
group by fullvisitorid, customDimensions.index, customDimensions.value

它给出的结果如下:

+---------------+------------------------+------------------------+
| fullvisitorid | customDimensions_index | customDimensions_value |
+---------------+------------------------+------------------------+
|             1 |                      2 | Bronze                 |
|             1 |                      3 | Yes                    |
|             2 |                      2 | Bronze                 |
|             2 |                      3 | No                     |
|             3 |                      2 | Bronze                 |
|             3 |                      3 | Yes                    |
|             4 |                      2 | Platinum               |
|             4 |                      3 | Yes                    |
+---------------+------------------------+------------------------+

我想要转置的值,其中 customDimension_index 2 是颜色,而 customDimension_value 3 是 yesno,所以结果应该是这样的:

+---------------+----------+-------+
| fullvisitorid |  color   | yesno |
+---------------+----------+-------+
|             1 | Bronze   | Yes   |
|             2 | Bronze   | No    |
|             3 | Bronze   | Yes   |
|             4 | Platinum | Yes   |
+---------------+----------+-------+

我可以单独拉一个然后另一个并加入 fullvisitorid,但希望能够以这种方式一步提取数据。谢谢!

【问题讨论】:

    标签: google-analytics google-bigquery


    【解决方案1】:

    2020-01 更新:#standard SQL 更新

    SELECT
      fullvisitorid,
      (SELECT value FROM UNNEST(customDimensions) WHERE index=2) color, 
      (SELECT value FROM UNNEST(customDimensions) WHERE index=3) yesno, 
    FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910`
    

    以前:

    Mosha 的回答是正确的,但我想添加这个,因为它利用了 GA 记录的嵌套性质:

    SELECT
      fullvisitorid,
      FIRST(IF(customDimensions.index=2, customDimensions.value, NULL)) WITHIN RECORD color, 
      FIRST(IF(customDimensions.index=3, customDimensions.value, NULL)) WITHIN RECORD yesno
    FROM [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
    WHERE customDimensions.index in (2,3)
    

    为什么:而不是运行 GROUP BY(它会消耗资源,因为它必须查看和分组可能具有相同 customerid 的任何记录), WITHIN RECORD 只查看各个行内。

    如果 customerid 有不止一行(例如,他们访问过一次 Bronze/Yes,然后访问 Platinum/No),结果将发出每一行和组合,而不仅仅是第一行。

    【讨论】:

    • 您好 Felipe,感谢您的回复!但是,我认为 group by 更适合我的需要,因为我的数据中的自定义维度是以用户为中心的,所以每个 fullvisitorid(理论上)都将具有相同的属性。我发现这个查询有欺骗性(每个 fullvisitorid 多行)——这在我需要按会话查看的未来情况下会很有帮助。再次感谢您的回复。
    【解决方案2】:

    解决办法如下:

    SELECT
      fullvisitorid,
      FIRST(IF(customDimensions.index=2, customDimensions.value, NULL)) color, 
      FIRST(IF(customDimensions.index=3, customDimensions.value, NULL)) yesno
    FROM [google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_20130910]
    where customDimensions.index in (2,3)
    group by fullvisitorid 
    

    它依赖于任何聚合函数,包括 FIRST,忽略 NULL 的事实

    【讨论】:

    • 对于已迁移到#standardSQL 的人:使用ANY_VALUE() 代替FIRST()。我相应地在下面更新了我的答案。
    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-08
    相关资源
    最近更新 更多