Sort by minimum value of two columns

后端 未结 13 1203
暖寄归人
暖寄归人 2021-02-03 17:13

I use SQL Server 2008 R2.

I need to sort a table by the minimal value of two columns.

The table looks like this:

ID: integer; 
Date         


        
13条回答
  •  清酒与你
    2021-02-03 17:53

    If you don't want to use Case statement in the Order By , then this is another approach, just moving the Case statement to Select

    SELECT Id, Date1, Date2 FROM 
     (SELECT Id, Date1, Date2
      ,CASE WHEN Date1 < Date2 THEN Date1 ELSE Date2 END as MinDate 
    FROM YourTable) as T
    ORDER BY MinDate
    

提交回复
热议问题