• I often think of APPLY as a JOIN on steroids. 😀

    Apply will allow you to use correlated subqueries as tables/views or columns from other tables as parameters of functions. I could go in depth but maybe you could read Paul White's articles to understand better: Understanding and Using APPLY (Part 1)[/url] & (Part 2)[/url]

    I can't promise that the query will run faster because I can't test it, but it should because all those repeated joins are not helping with the performance. You need to test it. Can you get rid of the test patients? Or use a table or column to remove the non SARGable filters?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2