Computing a moving maximum in BigQuery

后端 未结 3 959
长发绾君心
长发绾君心 2021-01-03 00:18

Given a BigQuery table with some ordering, and some numbers, I\'d like to compute a \"moving maximum\" of the numbers -- similar to a moving average, but for a maximum inste

3条回答
  •  余生分开走
    2021-01-03 00:19

    There's an example creating a moving using window function in the docs here.

    Quoting:

    The following example calculates a moving average of the values in the current row and the row preceding it. The window frame comprises two rows that move with the current row.

    #legacySQL
    SELECT
      name,
      value,
      AVG(value)
        OVER (ORDER BY value
              ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
        AS MovingAverage
    FROM
      (SELECT "a" AS name, 0 AS value),
      (SELECT "b" AS name, 1 AS value),
      (SELECT "c" AS name, 2 AS value),
      (SELECT "d" AS name, 3 AS value),
      (SELECT "e" AS name, 4 AS value);
    

提交回复
热议问题