TheSQLGuru (4/3/2013)
refactor your TVF to be an INLINE TVF.
I think it is an iTVF
Also push that SELECT ...(SELECT...) out like previous poster did.
I don't think that matters much, if at all.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden