【问题标题】:Transpose nested customDimension column data into rows Bigquery将嵌套的自定义维度列数据转置为行 Bigquery
【发布时间】:2020-01-27 23:42:23
【问题描述】:

我的 BigQuery 表如下所示

Fullvisitorid   CustomDimension.Index   CustomDimension.value
   123                1                       red
                      2                       blue
                      3                       green
   456                1                       red
                      3                       orange
                      4                       black

我希望我的最终输出如下所示

 Fullvisitorid   Color1     Color2
     123           red       green    
     456           red       orange

以下是我编写的查询,但我收到错误“未找到函数:FIRST”

SELECT
  fullvisitorid,
  FIRST(IF(customDimensions.index=1, customDimensions.value, NULL)) color1,
  FIRST(IF(customDimensions.index=3, customDimensions.value, NULL)) color2
  FROM `my_table`
  cross join
  unnest(customDimensions) customDimensions,
  unnest(hits) hits
  where customDimensions.index in (1,3)
   group by fullvisitorid

我发现了一个帮助我编写查询的类似问题:

[Transpose nested rows into columns in bigquery with google analytics data

我不确定为什么我的查询出现错误。 非常感谢任何帮助!

谢谢

【问题讨论】:

    标签: google-bigquery transpose unnest


    【解决方案1】:

    您现在正在使用#standardSQL - 这很好。

    ANY_VALUE()代替FIRST()

    我相应地更新了我在引用问题中的答案:

    【讨论】:

      【解决方案2】:

      您还可以创建一个用户定义的函数并在需要自定义维度时始终调用它:

      SELECT
        fullvisitorid,
        PATH.CUSTOM_DIMENSION_BY_INDEX(1, h.customDimensions) AS color1,
        PATH.CUSTOM_DIMENSION_BY_INDEX(3, h.customDimensions) AS color2,
        FROM `my_table`
        cross join
        unnest(customDimensions) customDimensions,
        unnest(hits) hits
        where customDimensions.index in (1,3)
         group by fullvisitorid
      

      其中 PATH.CUSTOM_DIMENSION_BY_INDEX(index, h.customDimensions) 是一个 UDF,其格式为:

      (SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index)
      

      你可以找到更多关于它的信息here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多