日期:2014-05-18  浏览次数:20464 次

怎么取一行的最大值
表如下

TR1     TR2     TR3     TR4     TR5     TR6     AR1     AR2     AR3     AR4     AR5     AR6
1         2         3         4         5         6         12       11       10       9         8         7

怎么取出
TR1——TR6的最大值6
AR1——AR6的最大值12


------解决方案--------------------
TR1 TR2 TR3 TR4 TR5 TR6 AR1 AR2 AR3 AR4 AR5 AR6
1 2 3 4 5 6 12 11 10 9 8 7

怎么取出
TR1——TR6的最大值6
AR1——AR6的最大值12
select id , max(data) as data from
(
select id = 'tr ' , tr1 as data
union all
select id = 'tr ' , tr2 as data
union all
select id = 'tr ' , tr3 as data
union all
select id = 'tr ' , tr4 as data
union all
select id = 'tr ' , tr5 as data
union all
select id = 'tr ' , tr6 as data
union all
select id = 'ar ' , ar1 as data
union all
select id = 'ar ' , ar2 as data
union all
select id = 'ar ' , ar3 as data
union all
select id = 'ar ' , ar4 as data
union all
select id = 'ar ' , ar5 as data
union all
select id = 'ar ' , ar6 as data
) t
group by id
------解决方案--------------------
selct
max(TR),max(AR)
from
(select TR1 as TR,AR1 as AR from t
union
select TR2,AR2 from t
union
select TR3,AR3 from t
union
select TR4,AR4 from t
union
select TR5,AR5 from t
union
select TR6,AR6 from t) a