首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何取一行的最大值

2012-02-17 
怎么取一行的最大值表如下TR1TR2TR3TR4TR5TR6AR1AR2AR3AR4AR5AR6123456121110987怎么取出TR1——TR6的最大值

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

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

热点排行