Hi @JDias ,
Sometimes using OUTER APPLY will perform a clustered index scan, in order to optimize performance, consider using LEFT JOIN instead of OUTER APPLY:
;WITH cte
as(SELECT Name,ROW_NUMBER() OVER(PARTITION BY M ORDER BY Yr DESC, Mth DESC) rr
FROM vFleetExpenses )
SELECT v.M,COmb,Mod,Loc,Initial,
Final,Kms_,R,Franq,Num_Fleet,Pneus,Cli,Active,Interv,
Cost1,Cost2,Name,Colab_Cod,col.colName
FROM tFleetCars AS v
LEFT JOIN cte c ON v.M=c.M AND c.rr=1
LEFT JOIN DB2.dbo.tCol AS col ON col.colID=Colab_Cod
Since there is no relevant sample data to test, the above code is untested code. If this does not solve your problem, please share us your table structure (CREATE TABLE …) and some sample data(INSERT INTO …) ,So that we’ll get a right direction and make some test.
Regards,
Echo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.