取一行多列数据中的最大值

    取某一列多行的最大值我们可以直接使用max函数,但是如果取某一行多个列中最大值,则不能直接用max函数了,需要做一些转换(嗯,就是传说中的行列转换)

    下表是一个学生成绩表,我们需要得到每个学生各科成绩中的最高分和最低分

----------- ----------- ----------- ----------- ----------- -----------
|学生号    | 语文      | 数学       | 英语      |  物理      |  化学   |    
----------- ----------- ----------- ----------- ----------- -----------
|1001      | 89        |   98      |    87     |     63     |     70   |
----------- ----------- ----------- ----------- ----------- -----------
|1002      |  81       |   87      |    79     |     97     |     87   |
----------- ----------- ----------- ----------- ----------- -----------
|1003      |  65       |   86      |    65     |     87     |     84   |
----------- ----------- ----------- ----------- ----------- -----------
|1004      |  87       |   82      |    89     |     84     |     76   |
----------- ----------- ----------- ----------- ----------- -----------
|1005      |  76       |   76      |    87     |     79     |     75   |
----------- ----------- ----------- ----------- ----------- -----------
|1006      |  90       |   68      |    67     |     94     |     90   |
----------- ----------- ----------- ----------- ----------- -----------
|1007      |  56       |   65      |    86     |     69     |     77   |
----------- ----------- ----------- ----------- ----------- -----------
|1008      |  78       |   100     |    83     |     86     |     93   |
----------- ----------- ----------- ----------- ----------- -----------

    在这里,我们只需要把每个学生的各科成绩转为一列,然后再MAX最大的分数就可以了。

  1. /***测试数据***/
  2. if object_id('[tb]'is not null drop table [tb]
  3. go
  4. create table [tb]([学生号] int,[语文] int,[数学] int,[英语] int,[物理] int,[化学] int)
  5. insert [tb]
  6. select 1001,89,98,87,63,70 union all
  7. select 1002,81,87,79,97,87 union all
  8. select 1003,65,86,65,87,84 union all
  9. select 1004,87,82,89,84,76 union all
  10. select 1005,76,76,87,79,75 union all
  11. select 1006,90,68,67,94,90 union all
  12. select 1007,56,65,86,69,77 union all
  13. select 1008,78,100,83,86,93
  14. /***查询***/ 
  15. select 
  16.   学生号,
  17.   语文,
  18.   数学,
  19.   英语,
  20.   物理,
  21.   化学,
  22.   (select max(t.a) from (select 语文 as a union select 数学 union select 英语 union select 物理 union select 化学) t) as [最高分],
  23.   (select min(t.a) from (select 语文 as a union select 数学 union select 英语 union select 物理 union select 化学) t) as [最低分]
  24. from tb
  25. /***结果***/
  26. 学生号      语文        数学         英语       物理       化学      最高分   最低分         
  27. ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
  28. 1001        89          98          87          63          70          98          63
  29. 1002        81          87          79          97          87          97          79
  30. 1003        65          86          65          87          84          87          65
  31. 1004        87          82          89          84          76          89          76
  32. 1005        76          76          87          79          75          87          75
  33. 1006        90          68          67          94          90          94          67
  34. 1007        56          65          86          69          77          86          56
  35. 1008        78          100         83          86          93          100         78
  36. (所影响的行数为 8 行)

 

你可能感兴趣的:(取一行多列数据中的最大值)