Sql-server – Calculate sum of N preceding values

excelsql server

I am write a SQL query to replicate below Excel result.

In Column A I have 252 values in ASC order from 10/30/2018 to 10/29/2019
In Column BV I have respective PL Number
I want to calculate TestPL column having value as Ignore first 3 PL Values and 4th Cell would be addition of B2:B5, 5th Cell would be B3:B6 and so on for all 251 values

TestPL should start from row 4 and should have sum of cells B2:B5, and row 5 would be B3:B6 and so on in a stream till row 252 which would B249:B252

enter image description here

Best Answer

Use a window function

...sum(PL) over(order by [Date] range between 3 preceding and current row)