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

后端 未结 19 2167
余生分开走
余生分开走 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条回答
  •  盖世英雄少女心
    2020-11-27 03:34

    This is brute force but works

     select case when col1 <= col2 and col1 <= col3 then col1
               case when col2 <= col1 and col2 <= col3 then col2
               case when col3 <= col1 and col3 <= col2 then col3
        as 'TheMin'
               end
    
    from Table T
    

    ... because min() works only on one column and not across columns.

提交回复
热议问题