
first_column_I_want_to_sum
is the column with the values you want to add up (sometimes this is an average)column_I_want_to_order_by
is the column you want to order by when defining your lookback windowN
is the number of previous rows you want to include in your calculation (don’t forget the current row is included, so for a four week lookback window, we’ll use N=3)second_column_I_want_to_sum
is optional, another column with values you want to add up to compare to your first column, like when calculating averages (which is how we use it in this example)
Here’s the SQL you can copy-paste to calculate rolling windows
Make sure you swap out the columns AND choose a number value forN
.