How to find the record in a table that contains the maximum value?

前端 未结 4 1525
没有蜡笔的小新
没有蜡笔的小新 2020-12-09 06:01

Although this question looks simple, it is kind of tricky.

I have a table with the following columns:

table A:
  int ID
  float value
  datetime date         


        
4条回答
  •  囚心锁ツ
    2020-12-09 06:06

    As long as the Date column is unique for each group I think something like this might work:

    SELECT A.ID, A.Value
    FROM A
      INNER JOIN (SELECT Group, MAX(Date) As MaxDate FROM A GROUP BY Group) B
        ON A.Group = B.Group AND A.Date = B.MaxDate
    

提交回复
热议问题