Hi,
Let’s assume I have table like below columns & related values.
Datebegin can start with any month(& year) & DateEnd could be end with any month(&year)
But, here I am doing ascending order with datebegin.
Data Available:
MemberID PlanId DateBegin DateEnd
1234 20 2009-10-01 00:00:00.000 2010-03-31 00:00:00.000
1234 20 2010-04-01 00:00:00.000 2011-05-31 00:00:00.000
1234 20 2011-06-01 00:00:00.000 2011-08-31 00:00:00.000
1234 1 2012-07-01 00:00:00.000 2029-12-31 00:00:00.000
I would like to generate result for three years 2010, 2011, 2012 with the eligibility plan number (for all 12 different months as different column),
If member is eligible on 1<sup>st</sup> of the month he is eligible for that related plan on that particular month.
You can see member 1234 was eligible till August – 2011 with plan id 20. (data line – 3)
But, next time member got eligibility on July – 2012 with different plan id.
So, member was not eligible between time frame Sep – 2011 to June – 2012. That’s why in result I would like to see blank as plan id.
Expected Result: memberid Year Jan Feb March April May June July Aug Sep Oct Nov Dec 1234 2010 20 20 20 20 20 20 20 20 20 20 20 20 1234 2011 20 20 20 20 20 20 20 20 1234 2012 1 1 1 1 1 1
This is what I got so far..
But this CTE is taking so long time for multiple member ids. ( I do have more than 10,000 members in my original query)
CREATE TABLE #temp_Data
(
memberid VARCHAR(20),
planid INT,
DATE_BEGIN DATETIME NOT NULL,
DATE_END DATETIME NOT NULL
)
INSERT INTO #temp_Data
SELECT '1234',20,'2009-10-01 00:00:00.000','2010-03-31 00:00:00.000'
UNION
SELECT '1234',20,'2010-04-01 00:00:00.000','2011-05-31 00:00:00.000'
UNION
SELECT '1234',20,'2011-06-01 00:00:00.000','2011-08-31 00:00:00.000'
UNION
SELECT '1234',1,'2012-07-01 00:00:00.000','2029-12-31 00:00:00.000'
CREATE TABLE #Dates
(
memberid VARCHAR(20),
planid INT,
DATE_BEGIN DATETIME NOT NULL,
DATE_END DATETIME NOT NULL
)
INSERT INTO #Dates
SELECT memberid,
planid,
MIN(DATE_BEGIN), -- I KNOW I AM DOING THIS WRONG WAY
MAX(DATE_END) -- FOR BOTH DATES
FROM #temp_Data
GROUP BY memberid,
planid
SELECT * FROM #Dates ORDER BY DATE_BEGIN
CREATE TABLE #Months
(
memberid VARCHAR(20),
[Year] INT,
[Month] INT,
planid INT,
startdate DATETIME,
endate DATETIME
)
;WITH MONTHS (memberid, planid, [Year], [Month], [Date], [End])
AS (SELECT memberid,
planid,
Datepart(YY, DATE_BEGIN) AS [Year],
Datepart(m, DATE_BEGIN) AS [Month],
DATE_BEGIN AS [Date],
Dateadd(s, -1, Dateadd(m, Datediff(m, 0, DATE_BEGIN) + 1, 0)) AS [End]
FROM #Dates
UNION ALL
SELECT D.memberid,
D.planid,
Datepart(YY, Dateadd(m, 1, [Date])),
Datepart(m, Dateadd(m, 1, [Date])),
Dateadd(m, 1, [Date]),
Dateadd(s, -1, Dateadd(m, Datediff(m, 0, Dateadd(m, 1, [Date])) + 1, 0)) AS [End]
FROM #Dates D
INNER JOIN MONTHS M
ON D.memberid = M.memberid
AND D.planid = M.planid
WHERE Cast(Datepart(yy, [Date]) AS VARCHAR(4))+ RIGHT('0' + Cast(Datepart(mm, [Date]) AS VARCHAR(2)), 2) < Cast(Datepart(yy, DATE_END) AS VARCHAR(4))+ RIGHT('0' + Cast(Datepart(mm, DATE_END) AS VARCHAR(2)), 2))
INSERT INTO #Months
SELECT memberid,
[Year],
[Month],
planid,
[date],
[end]
FROM MONTHS
WHERE [END] < '1/1/13'
AND [Date] >= '1/1/10'
OPTION (MAXRECURSION 0)
SELECT * FROM #Months
ORDER BY Year,Month
DROP TABLE #temp_Data,#Dates,#Months
ANK HIT - if reply helps, please mark it as ANSWER or helpful post