I want to query a giant table (SQL Server) for the latest version of a set of record e.g.
Table:
Id, key, Ver
1, A, 1
2, B, 1
3, A, 2
4, B, 2
5,B, 3
Result:
Id, Key, Ver
3, A, 2
5, B, 3
The join method mentioned in here will work but only work well if want to get the latest version of all keys but the join will become a too much overhead if we only care about a subset of keys. so I would like ask whether we should do a loop of
select top 1 * from table where key = 'A' order by ver desc
or
select max(ver), key from table where key in ('A', 'B') group by key
or there is better way doing so? cheers