我有兴趣使用自定义维度属性来吸引访问者,其中每一行都是唯一的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是color,而customDimension_value 3是yesno,所以结果看起来像这样:
+---------------+----------+-------+
| fullvisitorid | color | yesno |
+---------------+----------+-------+
| 1 | Bronze | Yes |
| 2 | Bronze | No |
| 3 | Bronze | Yes |
| 4 | Platinum | Yes |
+---------------+----------+-------+
我可以分别拉出另一个并加入fullvisitorid,但希望能够一步一步地以这种方式提取数据。谢谢!
答案 0 :(得分:5)
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)
原因:WITHIN RECORD只在单个行内查找,而不是运行GROUP BY(因为它必须查看并按任何可能具有相同customerid的记录进行分组,因此消耗资源)。
如果一个customerid有多行(例如,他们曾经访问过Bronze / Yes,之后是Platinum / No),结果将会发出每一行和每一行,而不是只有第一行。
答案 1 :(得分:3)
以下是解决方案:
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