WITH cte AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY val1 DESC) AS rn FROM MyTable ) SELECT * FROM cte WHERE rn = 1