--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/ ,如需转载,请注明出处,否则将追究法律责任。