Hi @Benniit ,
Welcome to Microsoft Q&A!
For this type of problem we recommend that you post CREATE TABLE statements for your tables together with INSERT statements with sample data, enough to illustrate all angles of the problem. We also need to see the expected result of the sample.
Suppose there is another column named Amount.
Please refer below and check whether it is helpful:
declare @Year int
declare @amount int
set @Year=YEAR(GETDATE())-5
set @amount=500
Select * from
(select * , row_number() over (partition by FileNo order by EndDate Desc) as row_number from RentPayment) as rows
where row_number=1 And StartYear < @Year and Amount=@amount
--or avoid using row_number()
Select *
from RentPayment
where StartYear < @Year and Amount=@amount
Best regards
Melissa
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.