ITPub博客

首页 > 应用开发 > IT综合 > 求一行中多个字段的最大值

求一行中多个字段的最大值

原创 IT综合 作者:polestar123 时间:2009-11-25 15:46:06 0 删除 编辑

例如表aa,有a,b,c,d 4个字段,要取每行中b,c,d 3个字段的最大值

A B C D
1 2 3 4
2 3 5 1
3 7 3 5

取出结果如下:

1 4
2 5
3 7

测试和实现如下:

create table aa
(
a int,
b int,
c int,
d int,
constraint pk_aa primary key( a )
)
insert into aa values(1,2,3,4);
insert into aa values(2,3,5,1);
insert into aa values(3,7,3,5);
commit;
select * from aa;
select aa.a, max(tmp.bb)
from aa,
(select a, b as bb
from aa
union all
select a, c as bb
from aa
union all
select a, d as bb from aa) tmp
where aa.a = tmp.a
group by aa.a

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70612/viewspace-1029046/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-05-22

  • 博文量
    110
  • 访问量
    438365