vbnetcoder
asked on
help modifying a query
I need help modifying this query
I want is not year to show month/year and group by that. Also, I want to show only 12 months from the start date to the end date.
DECLARE @END DATE = '12/01/2017'
DECLARE @Start DATE =DATEADD(month, -11, '12/01/2017')
;WITH _DateList([DATE]) AS(
SELECT @START
UNION ALL
SELECT DATES = DATEADD(MONTH,1,[DATE])
FROM _DateList
WHERE DATEADD(DAY,1,[DATE])<@END
)
SELECT DISTINCT
Yr = YEAR(DATE)
,ID = RTRIM(e.ID)
,NumMonths = COUNT(*)
INTO #MemMonths
FROM dbo.enrollkeys e
JOIN program p
ON p.programid = e.programid
JOIN _DateList
ON DATEADD(DAY,5,DATE) BETWEEN e.effdate AND e.termdate
GROUP BY RTRIM(e.ID), YEAR(DATE)
OPTION (MAXRECURSION 2000)
SELECT * FROM #MemMonths
I want is not year to show month/year and group by that. Also, I want to show only 12 months from the start date to the end date.
ASKER
I want it also to show the month and dates such as this
1/17 2/18 3/18 etc.
1/17 2/18 3/18 etc.
Change
GROUP BY RTRIM(e.ID), YEAR(DATE)
To group by by month rather than year.
GROUP BY RTRIM(e.ID), YEAR(DATE)
To group by by month rather than year.
Group by year,month
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ty
This way, when you change the declaration of end date, the start date will be 11 months earlier.
If you want twelve months, subtract 12
Or change and take away one year..
Open in new window