如果我不必每周手动输入日期字段,速度会快得多),但脚本“知道”我们所在的周数,并根据我们所在的周更新最近12周的数据。
下面是BigQuery标准SQL
WHERE _TABLE_SUFFIX
BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 12 * 7 + EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 2 DAY))
AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY))
#standardSQL
SELECT
FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 2 * 7 + EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 2 DAY)) first_day,
FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY)) last_day
-
first_day last_day
20171002 20171015
更新:
我在上面发布了我想要的第一个迭代答案
#standardSQL
SELECT
CONCAT(
FORMAT_DATE('Week %W %d %B %Y, ', first_day),
FORMAT_DATE('%d %B %Y, ', last_day),
FORMAT_DATE('%Y%m%d ', first_day),
FORMAT_DATE('%Y%m%d', last_day)
) wk
FROM (
SELECT
DATE_SUB(CURRENT_DATE(), INTERVAL 1 * 7 + EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 2 DAY) first_day,
DATE_SUB(CURRENT_DATE(), INTERVAL EXTRACT(DAYOFWEEK FROM CURRENT_DATE()) - 1 DAY) last_day
)
带输出
wk
Week 41 09 October 2017, 15 October 2017, 20171002 20171015