2016-09-08 49 views
0

以下查询运行并产生16行输出(通过包装在SELECT count(*) FROM (query)中进行验证)命中是重复记录。 hits.customDimensions在命中内重复。 customDimensions在主记录中重复。作用域聚合和WHERE作用域

SELECT 
    fullVisitorId, 
    visitId, 
    hits.page.pagePath, 
    hits.type, 
    FIRST(IF(customDimensions.index = 10, customDimensions.value, NULL)) WITHIN RECORD AS gacid, 
    FIRST(IF(hits.customDimensions.index = 11, hits.customDimensions.value, NULL)) WITHIN hits AS blogCategories 
FROM 
    [dataset.ga_sessions_20160902] 
WHERE 
    fullVisitorId ='55555555555' 

然而

SELECT 
    fullVisitorId, 
    visitId, 
    hits.page.pagePath, 
    hits.type, 
    FIRST(IF(customDimensions.index = 10, customDimensions.value, NULL)) WITHIN RECORD AS gacid, 
    FIRST(IF(hits.customDimensions.index = 11, hits.customDimensions.value, NULL)) WITHIN hits AS blogCategories 
FROM 
    [dataset.ga_sessions_20160902] 
WHERE 
    fullVisitorId ='55555555555' 
    AND hits.type = 'PAGE' 

失败

Cannot query the cross product of repeated fields customDimensions.index and hits.type. 

只返回一个(不平)记录和我的包裹数量,也没有给我真正的结果?为什么两个作用域集合可以在不同的作用域上工作,但最内层作用域上的WHERE失败?

+0

重要的是,你可以使用标记答案左侧的勾号在投票下面标记接受的答案。请参阅http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work#5235了解其重要性。答案投票也很重要。表决有用的答案。还有更多......当某人回答你的问题时,你可以查看该怎么做 - http://stackoverflow.com/help/someone-answers。 –

回答

1

,以避免产生交叉产品试试下面

SELECT 
    fullVisitorId, 
    visitId, 
    hits.page.pagePath, 
    hits.type, 
    FIRST(IF(customDimensions.index = 10, customDimensions.value, NULL)) WITHIN RECORD AS gacid, 
    FIRST(IF(hits.customDimensions.index = 11, hits.customDimensions.value, NULL)) WITHIN hits AS blogCategories 
FROM [dataset.ga_sessions_20160902] 
WHERE fullVisitorId ='55555555555' 
HAVING hits.type = 'PAGE' 

顺便说一下,在传统的SQL任何最SELECT产生扁平的结果(除非你写resuly到表相应的选项 - 大的结果和非平坦) - 这也解释了问题在你的例子中

+0

“,它解释了你的例子中的问题”不,它没有。这就是为什么我说我通过将它放在“select(count)(*)from()”查询中来测试大小。此外,没有交叉产品正在执行,引擎有它需要避免的上下文。 –

+0

我听到你:o(你的解决方法是否适合你?或者你只是有兴趣解释问题?最有可能来自GBQ团队 –

+0

另一种选择是明确的FLATTEN。我更感兴趣的是为什么会发生这种情况。我在其他地方发布了错误报告,我被告知我应该https://code.google.com/p/google-bigquery/issues/detail?id=694&thanks=694&ts=1473356481 –