T-SQL: Selecting Column Based on MAX(Other Column)

后端 未结 7 714
醉酒成梦
醉酒成梦 2020-11-27 12:06

I\'m hoping there\'s a simple way to do this without using a sub-query:

Scenario: You have \"TableA\" with columns \"Key\", \"SubKey\", and \"Value\". I need to ge

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 12:23

    SELECT MAX(Value)
    FROM TableA t1
    GROUP BY Key, SubKey
    HAVING SubKey = (SELECT MAX(SubKey) FROM TableA t2 WHERE t1.Key = t2.Key)
      AND Key = 1
    

提交回复
热议问题