This is more flexible because it doesn't require grouping by all columns:
;WITH x AS
(
SELECT ID, Name, RunDate, /* other columns, */
rn = ROW_NUMBER() OVER (PARTITION BY ID ORDER BY RunDate DESC)
FROM dbo.TableName
)
SELECT ID, Name, RunDate /* , other columns */
FROM x
WHERE rn = 1
ORDER BY ID;
(Since Name
doesn't really need to be grouped, and in fact shouldn't even be in this table, and the next follow-up question to the GROUP BY
solution is almost always, "How do I add <column x>
and <column y>
to the output, if they have different values and can't be added to the GROUP BY
?")