BigQuery过滤标准sql中的记录

时间:2017-02-01 10:26:21

标签: google-analytics google-bigquery

我正在计算在我们主页上提交邮政编码的所有访问者。我在遗留SQL中提出了以下查询:

SELECT fullVisitorId, visitStartTime
FROM TABLE_DATE_RANGE([ga_sessions_], TIMESTAMP('2017-01-29'), CURRENT_TIMESTAMP())
where hits.page.pagePath = '/broadband/'
and visitStartTime > 1483228800
and hits.type   = 'EVENT'
and hits.eventInfo.eventCategory = 'Homepage'
and hits.eventInfo.eventAction = 'Submit Postcode';

然后我想将它转换为标准SQL以在CTE中使用,并想出了一个看起来不正确的。

SELECT fullVisitorId, visitStartTime
FROM ``ga_sessions_*``, UNNEST(hits) as h
where 

_TABLE_SUFFIX > '2017-01-29'
AND h.page.pagePath = '/broadband/'
and visitStartTime > 1483228800
and h.type  = 'EVENT'
and h.eventInfo.eventCategory = 'Homepage'
and h.eventInfo.eventAction = 'Submit Postcode';

第一个处理327 MB并返回4117个结果,第二个处理6.98 GB并返回60745个结果。

我看过migration guide,但对我来说并不是很有帮助。

ga_sessions已将standard schema GA导入Bigquery。

2 个答案:

答案 0 :(得分:1)

这里发生的事情是_TABLE_SUFFIX > '2017-01-29' 是一个字符串所以当你这样做时:

DATE

由于字符串比较与数字比较不同,您最终会选择更多的表格。

解决这个问题的一种可能方法是将字符串解析为SELECT fullVisitorId, visitStartTime FROM `ga_sessions*`, UNNEST(hits) as h where parse_date("%Y%m%d", regexp_extract(_table_suffix, r'.*_(.*)')) >= parse_date("%Y-%m-%d", '2017-01-29') AND h.page.pagePath = '/broadband/' and visitStartTime > 1483228800 and h.type = 'EVENT' and h.eventInfo.eventCategory = 'Homepage' and h.eventInfo.eventAction = 'Submit Postcode'; 类型:

DATE

parse_date操作首先将字符串转换为REGEX_EXTRACT,然后进行比较。

另请注意,我将通配符选择更改为 ga_sessions ,然后使用%20我只考虑" _"之后的内容。字符。通过这样做,您就可以选择"日内"表格也是如此。

答案 1 :(得分:1)

看起来差异来自以下事实:使用标准SQL时,如果在OptionModule子句中static,则会在hits上展平表格,从而为此添加更多行结果。更等效的查询将是:

CROSS JOIN UNNEST(hits)