There was some bad data in there, that e didn't account for. I added the criteria below
;WITH CTE AS (
select pcode,pkey, RowEffectiveDate, rowenddate, RowEffectiveDateCalc, RowEndDateCalc,
RowEffectiveDateCalc CurRowEffectiveDateCalc, RowEndDateCalc CurRowEndDateCalc
from (
SELECT pcode,pkey, RowEffectiveDate, rowenddate
,cast(RowEffectiveDate as date) RowEffectiveDateCalc
,cast(rowenddate as date) RowEndDateCalc
, ROW_NUMBER() OVER(PARTITION BY pcode ORDER BY RowEffectiveDate, rowenddate, PKey) AS rn
FROM DimPro
--where PCode = 'TX5125'
) X
where rn = 1
union all
select c.pcode, c.pkey, c.RowEffectiveDate, c.rowenddate, c.RowEffectiveDateCalc, c.RowEndDateCalc,
c.RowEffectiveDateCalc CurRowEffectiveDateCalc, c.RowEndDateCalc CurRowEndDateCalc
from (
SELECT NextRow.PCode, NextRow.Pkey, NextRow.RowEffectiveDate, NextRow.rowenddate
,cast(NextRow.RowEffectiveDate as date) RowEffectiveDateCalc
,cast(NextRow.rowenddate as date) RowEndDateCalc
,cast(CurRow.RowEffectiveDate as date) CurRowEffectiveDateCalc
,cast(CurRow.rowenddate as date) CurRowEndDateCalc
,Row_Number() over (partition by NextRow.pcode order by NextRow.RowEffectiveDate, NextRow.rowenddate, NextRow.PKey) AS rn
FROM CTE CurRow
join DimPro NextRow
on CurRow.PCode = NextRow.PCode
and NextRow.RowEffectiveDate <= NextRow.rowenddate
and DateDiff(day, CurRow.RowEndDateCalc, cast(NextRow.RowEffectiveDate as date)) = 1
) c
where rn = 1
)
select * from CTE
order by 1, 2
For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/