Hi @pokiri ,
Welcome to the microsoft TSQL Q&A forum!
In terms of efficiency, temporary tables are a good first choice.You can try to use temporary tables instead of subqueries.
If you have any question, please feel free to let me know.
Regards
Echo
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.