Skip to content
Advertisement

Is there a way to find the highest 90 day total value for any 90 day period in the last year?

I’m thinking the only way to do it is to sum the values between (today – 365) and (today -65 + 90) then move on by 1 day each time, but that would be impractical. Is there a way around it?

Advertisement

Answer

If you have one row on each day:

select top (1) t.*
from (select t.*, sum(x) over (order by date rows between 89 preceding and current row) as sum_90
      from t
     ) t
order by sum_90 desc;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement