Sort by minimum value of two columns

后端 未结 13 1205
暖寄归人
暖寄归人 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 18:04
    SELECT ID, Date1, Date2
    FROM YourTable
    ORDER BY (SELECT TOP(1) v FROM (VALUES (Date1), (Date2)) AS value(v) ORDER BY v)
    

    Very similar to the @dyatchenko answer but without NULL issue

    0 讨论(0)
提交回复
热议问题