• Sean Lange (10/22/2015)


    Another highly likely issue for performance is the lack of normalization. You have to join to the same table over and over. If this was properly normalized that wouldn't be required.

    If OP posts something consumable (I can't access his source here) I'll take a shot with a rCTE to kick things off. There's not much in the way of business rules but it would be a start.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    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