Existing table table1, four int field f1,f2,f3,f4, now query to get the maximum value in the four column and only need to return the maximum value
Method One:
Select Case if F12>f34 then F12 else F34 end as Maxnum to
(Select Case when F1>f2 then F1 else F2 end as F12 , case when
F3>f4 then F3 else F4 end as F34 to table1) as T1 ORDER BY maxnum Desc
The effect is as follows:
Method Two:
Select top 1 * "from" (
select F1 as Maxnum, ' F1 ' as ColumnName from TABLE1
UNION
SELECT F2 as Maxnum, ' F2 ' as Col Umnname from TABLE1
Union
Select F3 as Maxnum, ' F3 ' as ColumnName from TABLE1
UNION
Select F4 as Maxnum, ' F 4 ' as ColumnName from TABLE1 ' as T-order by
T.maxnum DESC
The effect is as follows:
Note: There are 4 columns of data, here are three union connected four SELECT statements, each check a column, the last four columns of data into a row, and then check, it's good to check.
Knowledge additions to Method II: SQL Union and UNION ALL operators
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/