I have a table with the name of CouponData and Edate column.
I want to get last 12 months from current date. like from 21-sep-2018 to 01-sep-2017 how its possible using my query?
with yearlist as
(
select MONTH(GETDATE()) as MONTH
union all
select yl.MONTH - 1 as MONTH
from yearlist yl
where (yl.MONTH - 1 <= MONTH(GetDate())) and (yl.MONTH - 1 >= MONTH(DATEADD(MONTH ,-8,GETDATE())))
)
Select month,(Select IsNull(Sum(BillAmount),0) from CouponData Where month(EDate) = YL.month)
as Collection,(Select IsNull(Sum(AdultsQty+ChildQty),0) from CouponData Where month(EDate) = YL.month)
as PaxTotal from yearlist YL order by MONTH asc
datedifffunction. - Arvo