如何对 google 云 sql 或 python 数据框中的每滚动 3 行进行分组?

How do I group by for every rolling 3 rows in google cloud sql or python dataframe?

假设我有一个数据集。

Year | Month | Count
2017 | 1     | 3
2017 | 2     | 4
2017 | 3     | 2
2017 | 4     | 1
2017 | 5     | 5
2017 | 6     | 6

我希望按结果分组为

group 1    | total count
month1,2,3 |    9
month2,3,4 |    7
month3,4,5 |    8
...

我想知道在 google 云 SQL 中有没有办法做到这一点?如果没有,我可以在 python 数据框中执行此操作吗?

您可以使用 lead()sum()。假设 month 是一个字符串:

select concat(month, ',', lead(month) over (order by year, month), ',',
              lead(month, 2) over (order by year, month)
             ),
       sum(count) over (order by year, month rows between current row and 2 following) as total
from t;

或:

with t as (
      select 2017 as year, 1 as month, 3 as count union all
      select 2017, 2, 4 union all
      select 2017, 3, 2 union all
      select 2017, 4, 1 union all
      select 2017, 5, 5 union all
      select 2017, 6, 6
     )
select array_to_string(array_agg(cast(count as string)) over (order by year, month rows between current row and 2 following), ','),
       sum(count) over (order by year, month rows between current row and 2 following)
from t;

array_agg()用作window函数可能比lead()更麻烦一点。不过再多几个元素,就简单多了。