What's the best way to select the minimum value from several columns?

后端 未结 19 2126
余生分开走
余生分开走 2020-11-27 02:47

Given the following table in SQL Server 2005:

ID   Col1   Col2   Col3
--   ----   ----   ----
1       3     34     76  
2      32    976     24
3       7             


        
19条回答
  •  萌比男神i
    2020-11-27 03:48

    There are likely to be many ways to accomplish this. My suggestion is to use Case/When to do it. With 3 columns, it's not too bad.

    Select Id,
           Case When Col1 < Col2 And Col1 < Col3 Then Col1
                When Col2 < Col1 And Col2 < Col3 Then Col2 
                Else Col3
                End As TheMin
    From   YourTableNameHere
    

提交回复
热议问题