SQL MAX of multiple columns?

后端 未结 22 1855
伪装坚强ぢ
伪装坚强ぢ 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:32

    From SQL Server 2012 we can use IIF.

     DECLARE @Date1 DATE='2014-07-03';
     DECLARE @Date2 DATE='2014-07-04';
     DECLARE @Date3 DATE='2014-07-05';
    
     SELECT IIF(@Date1>@Date2,
            IIF(@Date1>@Date3,@Date1,@Date3),
            IIF(@Date2>@Date3,@Date2,@Date3)) AS MostRecentDate
    

提交回复
热议问题