MSSQL – group by latest of each type

MSSQL – group by latest of each type

SELECT t.*
FROM (
    SELECT *, ROW_NUMBER() OVER (
        PARTITION BY {fieldtobegrouped}
        ORDER BY {fieldtosortitemswithingroupby} DESC
    ) AS [RowNum]
    FROM {tablename}
) AS t
WHERE t.[RowNum] = 1
ORDER BY {fieldtosortfinalgroupedlistby} ASC</code>

Solution source: http://stackoverflow.com/questions/3804561/mssql-only-last-entry-in-group-by-with-id#answer-3804617