2017-10-06 44 views
1

具有索引6customDimensions对应于会话和命中级别上的UUID。查询会话中的自定义维度以及命中级别

在会话水平,我可以使用下列标准的SQL查询来检索的UUID:

CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
    (SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index) 
); 

SELECT 
customDimensionByIndex(6, customDimensions) AS session_uuid -- Customer UUID 
FROM `94860076.ga_sessions_20170822` 
limit 10 

同样,在命中水平,我可以使用:

CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
    (SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index) 
); 

SELECT 
customDimensionByIndex(6, hits.customDimensions) AS hit_uuid -- Customer UUID 
FROM `94860076.ga_sessions_20170822`, unnest(hits) as hits 
limit 10 

不过,我不在同一个查询中使用两者。例如,我想要一个结果集,其中每行对应于会话,列为session_uuidarray_of_hit_uuids。这怎么能实现?

回答

1

下面是BigQuery的标准SQL

#standardSQL 
CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
    (SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index) 
); 
SELECT * 
FROM (
    SELECT 
    customDimensionByIndex(6, customDimensions) AS session_uuid, 
    ARRAY(
     SELECT val FROM (
     SELECT customDimensionByIndex(6, hits.customDimensions) AS val 
     FROM UNNEST(hits) AS hits 
    ) 
     WHERE NOT val IS NULL 
    ) AS hit_uuid 
    FROM `94860076.ga_sessions_20170822` 
) 
WHERE session_uuid IS NOT NULL 
LIMIT 10 

您可以通过公共数据集进行测试

#standardSQL 
CREATE TEMP FUNCTION customDimensionByIndex(indx INT64, arr ARRAY<STRUCT<index INT64, value STRING>>) AS (
    (SELECT x.value FROM UNNEST(arr) x WHERE indx=x.index) 
); 
SELECT * 
FROM (
    SELECT 
    customDimensionByIndex(2, customDimensions) AS session_uuid, 
    ARRAY(
     SELECT val FROM (
     SELECT customDimensionByIndex(1, hits.customDimensions) AS val 
     FROM UNNEST(hits) AS hits 
    ) 
     WHERE NOT val IS NULL 
    ) AS hit_uuid 
    FROM `google.com:analytics-bigquery.LondonCycleHelmet.ga_sessions_20130910` 
) 
WHERE session_uuid IS NOT NULL 
LIMIT 10 
+0

岂不内连接更快? – Dror

+0

很可能不是 - 但你可以试试:)其间,你有没有尝试过?它为你工作? –

+0

它似乎在伎俩,但最终,我用了一种不同的方法。您的方法生成的数组不是我想要的。你的方法产生一个以会话为中心的视图,其中点击被映射到数组。对? – Dror