以下查询效果很好,每个月的所有星期一显示2018年。
select dt3 from unnest(generate_date_array(date_trunc('2018-1-1',week(monday)),'2019-1-1',interval 1 week))dt3
但在上述查询中,我想用 start_date 和'2019-替换 date_trunc('2018-1-1',周(星期一)) 1-1',间隔1周, end_date 。所以我写了下面的查询,但它没有用。任何建议。
select dt2 from unnest(generate_date_array(start_date,end_date))dt2 from(select date_add(start_date,interval 371 day)end_date,start_date from (select date_trunc('2018-1-1',week(monday))start_date))
答案 0 :(得分:1)
#standardSQL
WITH dates AS (
SELECT 1 seq, DATE_TRUNC('2018-1-1',week(monday)) start_date, DATE '2019-1-1' end_date
)
SELECT dt2
FROM dates, UNNEST(GENERATE_DATE_ARRAY(start_date, end_date, INTERVAL 1 WEEK)) dt2
如果您只想要start_date(如问题中的第二个查询):
#standardSQL
WITH dates AS (
SELECT 1 seq, DATE_TRUNC('2018-1-1',week(monday)) start_date
)
SELECT dt2
FROM dates, UNNEST(GENERATE_DATE_ARRAY(start_date, DATE_ADD(start_date,INTERVAL 371 DAY), INTERVAL 1 WEEK)) dt2