Distinct by ContractId
?
You can use ROW_NUMBER
with PARTITION BY ContractId
and select only the first of each group:
WITH CTE AS
(
SELECT C.*,
ROW_NUMBER() OVER (PARTITION BY ContractId Order By ContractDate DESC)AS RN
FROM Tbl_Contract C
)
SELECT * FROM CTE WHERE RN = 1
Note that you can add the joins in the cte without any problem. I've omitted them for improved readability.