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

后端 未结 7 635
醉酒成梦
醉酒成梦 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:45

    In case of multiple keys using a CTE:

    WITH CTE AS
    (
        SELECT key1, key2, MAX(subkey) AS MaxSubkey
        FROM TableA 
        GROUP BY key1, key2
    )
    SELECT a.Key1, a.Key2, a.Value
    FROM TableA a
        INNER JOIN CTE ON a.key1 = CTE.key1 AND a.key2 = CTE.key2 AND
                          a.subkey = CTE.MaxSubkey
    
    0 讨论(0)
提交回复
热议问题