Hi @Nimisha Vernekar
I did a local test,you can take a reference,my approach is to filter out the data not in the fit month
Test:January to December
January to October
Steps:
My test table is like this:
Create parameters: pastyearstartmonth AND pastyearendmonth
Dateset :
;with cte as(select *,yearpart= DATEPART(yy,years),monthpart = DATEPART(mm,years) from tableproduct)
SELECT * FROM cte WHERE yearpart = (SELECT currentyear = datepart(YY,GETDATE())) AND monthpart BETWEEN (@pastyearstartmonth) AND (@pastyearendmonth)
UNION ALL
SELECT * FROM cte WHERE yearpart = (SELECT currentyear = datepart(YY,GETDATE())-1) AND monthpart BETWEEN (@pastyearstartmonth) AND (@pastyearendmonth)
UNION ALL
SELECT * FROM cte WHERE yearpart = (SELECT currentyear = datepart(YY,GETDATE())-2)
Report is like this:
Hope this will help you.
Best Regards,
Isabella