• Not knowing what changes you made to your code I made a simple change, I moved your CTE pdtmstr into a select into statement to populate a temporary table called #pdtmstr. I then replaced the reference to the CTE with this temporary table.