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

后端 未结 19 2174
余生分开走
余生分开走 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:33

    For multiple columns its best to use a CASE statement, however for two numeric columns i and j you can use simple math:

    min(i,j) = (i+j)/2 - abs(i-j)/2

    This formula can be used to get the minimum value of multiple columns but its really messy past 2, min(i,j,k) would be min(i,min(j,k))

提交回复
热议问题