Вот ваш запрос. (Применимо для MS SQL Server)
- Получите ваш yyyyMM
dateadd(month, datediff(month, 0, [date]), 0)
- используйте
sum over
ваши данные за 3 месяца на основе yyyyMM - затем
group by
id
select id, dateadd(month, datediff(month, 0, [date]), 0) as yyyymm
,sum(sum([value])) over
(partition by id order by
dateadd(month, datediff(month, 0, [date]), 0)
rows between 2 preceding and current row) as Rolling3Months
from test
group by id, dateadd(month, datediff(month, 0, [date]), 0)
order by id, yyyymm
Попробуйте SQLFIDDLE