gpt4 book ai didi

google-bigquery - BigQuery 获取本周和上周的数据

转载 作者:行者123 更新时间:2023-12-01 11:15:27 26 4
gpt4 key购买 nike

假设我有一个查询:

SELECT ga_channelGrouping, ga_sourceMedium, SUM(ga_sessionDuration)/SUM(ga_sessions) as avg_sessionDuration 

FROM database.table

group by ga_channelGrouping, ga_sourceMedium

.

如何从 BigQuery中选择上周和本周的数据如果我有 DATE看起来像这样的列 2018-06-19 11:00:00 UTC .

最佳答案

DATE_TRUNC 是一个有用的函数来获取一周的开始,而 DATE_SUB 可以让你到达上周

DATE_TRUNC

DATE_SUB

SELECT if(date(date) >= DATE_TRUNC(current_date(), WEEK(MONDAY)),"This Week","Last Week") weekPeriod, 
ga_channelGrouping,
ga_sourceMedium,
SUM(ga_sessionDuration)/SUM(ga_sessions) as avg_sessionDuration

FROM database.table
WHERE date(date) >= DATE_SUB(DATE_TRUNC(current_date(), WEEK(MONDAY)), INTERVAL 1 WEEK)
group by weekPeriod, ga_channelGrouping, ga_sourceMedium

如果您的一周从星期日开始,只需更改 周(周一)周(周日)

关于google-bigquery - BigQuery 获取本周和上周的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com