help modifying a query

I need help modifying this query

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

Open in new window


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.
vbnetcoderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Change your declaration of @start and use @end instead
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..
DECLARE @ENDDATE = '12/01/2017'
DECLARE @StartDATE =DATEADD(month, -12, @ENDDATE)

Open in new window

vbnetcoderAuthor Commented:
I want it also to show the month and dates such as this

1/17  2/18  3/18 etc.
arnoldCommented:
Change
GROUP BY RTRIM(e.ID), YEAR(DATE)
To group by by month rather than year.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

arnoldCommented:
Group by year,month
Mark WillsTopic AdvisorCommented:
A few little things to change...

The recursive part you are doing a dateadd(month,1... but looping for date add(day,1... and later joining to it with a dateadd(day,5... seems to be a little bit inconsistent and needs some attention.... I think that the recursive part could use day or month, so I defaulted to the common denominator / theme of month.

I understand why you were subtracting 11 - to keep it in the same year, but you cold also manage with a change to the where clause, it will end up counting the number of months correctly.... I will keep it at 11...

DECLARE @END DATE = '20171201'
DECLARE @Start DATE =DATEADD(month, -11, @end)


;WITH _DateList([DATE]) AS
( SELECT @START [DATE]
  UNION ALL
  SELECT DATEADD(month,1,[DATE])   
  FROM _DateList 
  WHERE [DATE]<=@END
)
 SELECT DISTINCT   -- the distinct shouldnt be needed here....
        Yr         =      YEAR([DATE])
       ,mth        =      MONTH([DATE])     
       ,ID         =      RTRIM(e.ID)
       ,NumMonths  =      COUNT(*)
 INTO #MemMonths
 FROM  dbo.enrollkeys e 
 JOIN  program p ON p.programid = e.programid
 JOIN _DateList d ON d.[DATE] BETWEEN e.effdate AND e.termdate

 GROUP BY RTRIM(e.ID), YEAR([DATE]),MONTH([Date]) 
 OPTION (MAXRECURSION 0)


SELECT * FROM  #MemMonths

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vbnetcoderAuthor Commented:
ty
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.