Секрет ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING
в предложении OVER()
:
WITH data AS (
SELECT *
FROM UNNEST([STRUCT(2010 AS year, 'x' AS model, 2 AS price), (2011, 'x', 4), (2012, 'x', 8), (2013, 'x', 16), (2015, 'x', 32)])
)
SELECT *
, AVG(price) OVER(PARTITION BY model ORDER BY year ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) avg
, STDDEV(price) OVER(PARTITION BY model ORDER BY year ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING ) stddev
FROM data