I/P:- Id ForeignId Val1 1 1 123 2 1 234 3 1 345 4 2 456 5 2 567 O/P:- Id ForeignId Val1 3 1 345 5 2 567
SELECT * FROM MyTable m JOIN (SELECT MAX(Id) AS MaxId FROM MyTable GROUP BY ForeignId) j ON m.Id = j.MaxId
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER(PARTITION BY ForeignId ORDER BY Val1 DESC) AS RowNo FROM YourTableName ) AS A WHERE A.RowNo = 1
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)