2017-05-03 68 views
1

'hits'是重复记录。 'hits'下的'hits.customDimensions'也是如此。如何查询Google Big Query标准中重复记录中的重复记录SQL

我有这个谷歌大查询在标准SQL:

SELECT  

     visitNumber, 
     h.time, h.hour, h.minute, 
     h.page.PagePath, 


     h.customDimensions.value as language, /* not working */ 


      from 
       `550335029.ga_sessions_*` , UNNEST(hits) as h 
      where    
       h.customDimensions.index = 3 /* not working */ 

我正在寻找正确的语法来访问hits.customDimensions.index和hits.customDimensions.value。如果我删除了两个“不工作”的行,查询就会运行。

错误看起来是这样的:

GenericGBQException: Reason: invalidQuery, Message: Cannot access field customDimensions on a value with type ARRAY<STRUCT<hitNumber INT64, time INT64, hour INT64, ...>> at [40:46] 

回答

1

下面尝试使用BigQuery的标准SQL

SELECT 
    visitNumber, 
    h.time, 
    h.hour, 
    h.minute, 
    h.page.PagePath, 
    d.value AS language 
FROM 
    `550335029.ga_sessions_*`, 
    UNNEST(hits) AS h, 
    UNNEST(h.customDimensions) AS d 
WHERE d.index = 3 
1

我还发现,您可以使用标准的SQL时应遵守伟大的表演提升,并避免一些​​操作(但这取决于你的操作)。

作为一个例子,这是解决这个的另一种方式:

SELECT 
    visitNumber, 
    h.time, 
    h.hour, 
    h.minute, 
    h.page.PagePath, 
    (select value from unnest(h.customDimensions) where index = 3) AS LANGUAGE 
FROM 
    `550335029.ga_sessions_*`, 
    UNNEST(hits) AS h 
WHERE 1 = 1 
AND REGEXP_EXTRACT(_table_suffix, r'.*_(.*)') BETWEEN FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)) 
AND FORMAT_DATE("%Y%m%d", DATE_SUB(CURRENT_DATE(), INTERVAL 0 DAY)) 
and exists(select 1 from unnest(h.customDimensions) dim where dim.index = 3) 

你不会找到太多的区别你现在正在做什么,但它也同样吸引记住不同的技术在BQ一样操作他们最终可以使您的查询速度提高数十倍。

+0

我很想知道UNNEST在这里避免了什么,乍一看它看起来像你在做与上面相同的UNNESTing,只是将其中一个移动到子查询中。 –

+0

如果您在整个数据集上应用UNNEST,那么未占用阵列外的所有密钥都将被复制。通过做我所做的避免,即只有数组'h.customDimension'没有出现,并且不会发生外部键的重复。 –