一条SQL语句实现:一行多个字段数据的最大值。
原帖:http://community.csdn.net/Expert/topic/5403/5403570.xml?temp=.6892511
在回帖中发现有一方法不错,那就是marco08(天道酬勤) 的回帖,如下:
1create table T(A decimal(10,1), B decimal(10,1), C decimal(10,1), D decimal(10,1), E decimal(10,1))
2insert T select -21.5,-15.0,-5.0, null, null
3union all select -5.5,-11.5,null, null, null
4union all select -1.0,-16.5,-10.5, null, null
5
6
7select *,
8max_value=(
9select max(A) from
10(
11select A
12union all
13select B
14union all
15select C
16union all
17select D
18union all
19select E
20)tmp)
21from T
22
2insert T select -21.5,-15.0,-5.0, null, null
3union all select -5.5,-11.5,null, null, null
4union all select -1.0,-16.5,-10.5, null, null
5
6
7select *,
8max_value=(
9select max(A) from
10(
11select A
12union all
13select B
14union all
15select C
16union all
17select D
18union all
19select E
20)tmp)
21from T
22
--result
A B C D E max_value
------------ ------------ ------------ ------------ ------------ ------------
-21.5 -15.0 -5.0 NULL NULL -5.0
-5.5 -11.5 NULL NULL NULL -5.5
-1.0 -16.5 -10.5 NULL NULL -1.0
(3 row(s) affected)
这一方法,自我感觉不错,还真的第1次看到这样的写法。原来SQL里面还可以实现这样的写法,又学到了一点知识。