Reputation: 833
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
Upvotes: 2
Views: 7524
Reputation: 23827
with totals as
(
Select month(edate) as [month],
Sum(BillAmount) as bamt,
Sum(AdultsQty+ChildQty) as qty
from CouponData
Where edate >= dateadd(year,-1,getdate())
group by month(edate)
)
select [Month],Isnull(bAmt,0) as Collection, qty as PaxTotal
from totals
order by [Month];
To have all months:
WITH totals
AS (SELECT MONTH(edate) AS [month],
SUM(BillAmount) AS bamt,
SUM(AdultsQty + ChildQty) AS qty
FROM CouponData
WHERE edate >= DATEADD(YEAR, -1, GETDATE())
GROUP BY MONTH(edate))
SELECT m.mno AS [Month],
ISNULL(bamt, 0) AS Collection,
ISNULL(qty, 0) AS PaxTotal
FROM
(
VALUES
(1),(2),(3),
(4),(5),(6),
(7),(8),(9),
(10),(11),(12)
) m (mno)
LEFT JOIN totals t
ON t.month = m.mno
ORDER BY m.mno;
Upvotes: 0
Reputation: 3701
you can't work just with months, you need to consider the year too
with yearlist as
(
select MONTH(GETDATE()) + 12 * year(GETDATE()) as MONTH
union all
select yl.MONTH - 1 as MONTH
from yearlist yl
where yl.MONTH - 1 >= MONTH(GETDATE()) + 12 * year(GETDATE()) -12
)
Select month /12 as 'year', month % 12 as 'month',(Select IsNull(Sum(BillAmount),0) from CouponData Where month(EDate)+ YEAR(EDate) * 12 = YL.month)
as Collection,(Select IsNull(Sum(AdultsQty+ChildQty),0) from CouponData Where month(EDate) + YEAR(EDate) * 12 = YL.month)
as PaxTotal from yearlist YL order by MONTH asc ;
Upvotes: 0
Reputation: 168
You can get date range by using following query.replace start date.
DECLARE @Joiningdate DATE
Declare @StartDate DATE= cast('2017/09/01' as datetime)
Declare @EndDate DATE=getdate()
SET @StartDate = dateadd(DAY,-365 ,getdate())
print @StartDate
;with DateRange As
(
SELECT TOP (DATEDIFF(DAY, @StartDate, @EndDate) + 1)
Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @StartDate),'' as NoOfday
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
)select * from DateRange
Upvotes: 0
Reputation: 1270553
I am a bit unclear on what your range means. It is not exactly 12 months. But, I suggest that you use dates:
with months as (
select dateadd(day, 1, eomonth(getdate(), -1)) as yyyymm
union all
select dateadd(month, -1, yyyymm)
from months m
where yyyymm >= dateadd(-13, month, getdate())
)
Select m.yyyymm, coalesce(sum(BillAmount), 0) as Collection,
coalesce(Sum(AdultsQty + ChildQty), 0) as PaxTotal
from months m left join
CouponData cd
on edate >= m.yyyymm and edate < dateadd(1, month, m.yyyym)
group by m.yyyymm
order by m.yyyymm;
Upvotes: 4