开发者

SQL Select Command Syntax Help

开发者 https://www.devze.com 2023-04-03 17:16 出处:网络
Hopefully thi开发者_开发知识库s is pretty simple, but I\'m having some trouble. I have a table that has multiple fields but basically the two that matter are ID and year. A single ID can exist in many

Hopefully thi开发者_开发知识库s is pretty simple, but I'm having some trouble. I have a table that has multiple fields but basically the two that matter are ID and year. A single ID can exist in many years. How do I set up a select statement (Which I'm ultimately using in a join in another statement) so that I can retrieve all of the distinct IDS with no duplicates for the top year that they exist for?

If there is a set of records like this:

ID - Year
55 - 2000
55 - 2001
56 - 2000
56 - 2002

So basically I want something like this returned:

55 - 2001
56 - 2002

Help?


SELECT ID, MAX(Year) FROM MyTable GROUP BY ID

References:

  • SELECT (Transact-SQL)
    • GROUP BY (Transact-SQL)
  • Aggregate Functions (Transact-SQL)
    • MAX (Transact-SQL)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号