SQL Server: Help in writing a query to get 3 rolling months average for a table
select month
     , year
     , vol
   --, sum(vol) over (partition by costx, name order by month,year ROWS BETWEEN 2 PRECEDING AND current row) sumnetvol,
     , avg(vol) over (partition by costx, name order by month,year ROWS BETWEEN 2 PRECEDING AND current row) '3monthra'
     , name
     , cnt
     , costx                                                                       
from (
SELECT month(convert(datetime, cast(monthnumber as nvarchar(10))+'01')) as [Month]
     , year(convert(datetime,cast(monthnumber as nvarchar(10))+'01')) as [Year]
     , sum(SumOfNetAmount) as vol
     , count(*) as cnt
     , costx
     , name 
FROM [ngl_OTB] 
GROUP BY costx
       , MONTH(convert(datetime,cast(monthnumber as nvarchar(10))+'01'))
       , YEAR(convert(datetime,cast(monthnumber as nvarchar(10))+'01'))
       , name
       , SumOfNetAmount)s 
where costx = '1000.002' and name = 'ngl-raw' order by month,year