SQL MAX of multiple columns?

后端 未结 22 1853
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 02:03

How do you return 1 value per row of the max of several columns:

TableName

[Number, Date1, Date2, Date3, Cost]

I n

22条回答
  •  攒了一身酷
    2020-11-22 02:19

    SELECT 
        CASE 
            WHEN Date1 >= Date2 AND Date1 >= Date3 THEN Date1 
            WHEN Date2 >= Date3 THEN Date2 
            ELSE Date3
        END AS MostRecentDate 
    

    This is slightly easier to write out and skips evaluation steps as the case statement is evaluated in order.

提交回复
热议问题