Вместо того, чтобы прыгать 53 строки, go и искать точный 1 год go дата:
WITH data AS (
SELECT *
FROM `fh-bigquery.weather_gsod.all`
WHERE date BETWEEN '2018-12-01' AND '2020-02-24'
AND name LIKE 'SAN FRANCISCO INTERNATIONAL A'
), main_query AS (
SELECT name, date, temp
, ARRAY_AGG(STRUCT(date, temp)) OVER(PARTITION BY name ORDER BY date ROWS BETWEEN 366 PRECEDING AND 310 PRECEDING ) over_array
FROM data a
)
SELECT * EXCEPT(over_array)
, (SELECT temp FROM UNNEST(over_array) WHERE date=DATE_SUB(a.date, INTERVAL 1 year)) prev_year
FROM main_query a
ORDER BY name, date DESC
Я делал это с днями - вы можете сделать то же самое с неделями.
Решено за недели:
WITH data AS (
SELECT ROUND(AVG(temp),1) temp, DATE_TRUNC(date, week) week, name
FROM `fh-bigquery.weather_gsod.all`
WHERE date BETWEEN '2018-12-01' AND '2020-02-24'
AND name LIKE 'SAN FRANCISCO INTERNATIONAL A'
GROUP BY name, week
), main_query AS (
SELECT name, week, temp
, ARRAY_AGG(STRUCT(week, temp))
OVER(PARTITION BY name ORDER BY week
ROWS BETWEEN 53 PRECEDING AND 40 PRECEDING ) over_array
FROM data a
)
SELECT * EXCEPT(over_array)
, (SELECT temp FROM UNNEST(over_array)
WHERE EXTRACT(YEAR FROM week)+1=EXTRACT(YEAR FROM a.week)
AND EXTRACT(WEEK FROM week)=EXTRACT(WEEK FROM a.week)) prev_year
FROM main_query a
ORDER BY name, week DESC