Paul, if you wouldn't mind running through your sample script and correcting the errors which Steve kindly pointed out, I'll take a look later today.
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