按月分组Google Analytics / BigQuery

时间:2018-03-02 11:34:12

标签: google-analytics google-bigquery

我对BigQuery很陌生,并且对使用Google Analytics数据(StandardSQL)对Date进行分组提出了疑问。数据目前处于每日水平,如何将其汇总到年/月水平?

期望的结果:年/月水平的数据+仅过去12个月的选择。

#StandardSQL
SELECT
TIMESTAMP(PARSE_DATE('%Y%m%d',date)) as Date,
SUM(totals.visits) AS Visits,
totals.timeOnSite AS TimeOnSite,
totals.newVisits AS NewVisit
FROM
`XXXX.ga_sessions_20*`
WHERE
_TABLE_SUFFIX >= '180215'
GROUP by
Date,
TimeOnSite,
NewVisit

提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以使用DATE_TRUNC函数(https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#date_trunc):

#StandardSQL
SELECT
DATE_TRUNC(PARSE_DATE('%Y%m%d',date), MONTH) as MonthStart,
SUM(totals.visits) AS Visits,
totals.timeOnSite AS TimeOnSite,
totals.newVisits AS NewVisit
FROM
`XXXX.ga_sessions_20*`
WHERE
_TABLE_SUFFIX >= '180215'
GROUP by
Date,
TimeOnSite,
NewVisit

答案 1 :(得分:0)

当您将数据选择限制为上一年时,如果您的数据库中有一个记录访问日期的字段,您可以使用此查询每月获得汇总结果:

var row = $('#' + id).closest('tr'); 'XXXX.ga_sessions_20 *'#StandardSQL SELECT EXTRACT(MONTH FROM 'date_field_of_the_visit') AS Month, sum(totals.visits) AS Visits FROM