ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 找出最大值

找出最大值

原创 Linux操作系统 作者:keeking 时间:2009-08-08 18:57:21 0 删除 编辑

--c为公式字段,当输入a,b时,c自动为a,b中的最大值
create table test1
(
a int,
b int,
c as case when a-b>=0 then a else b end
)

insert into test1 values(2,4)

---查出各行的最大值

create table testdate(id int not null primary key,col1 int,col2 int,col3 int)

insert into testdate(id,col1,col2,col3)
select 1,20,60,55
Union all
select 2,66,77,88
union all
select 3,88,90,67
union all
select 4,77,88,99

 

with t as
(
select id,col1 col from testdate
union all
select id,col2 from testdate
union all
select id,col3 from testdate
)
select id,max(col) col from
t
group by id;
---------------------------------
select id, maxcol=max(val)
from testdate
unpivot(val for cols in([col1],[col2],[col3])) as UP
group by id

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

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

注册时间:2009-07-23

  • 博文量
    194
  • 访问量
    287361