日期:2014-05-17 浏览次数:20566 次
create table Table1(ID int, Past int, Now int, Future int)
insert into table1 values(1 , 0 , 1 , 5)
insert into table1 values(2 , 1 , 3 , 0)
insert into table1 values(3 , 2 , 4 , 3)
go
select id , case when Past >= Now and Past >=Future then Future
when Now >= past and Now >=Future then Now
when Future>=Past and Future >=Now then Future
end [Max]
from table1
/*
id Max
----------- -----------
1 5
2 3
3 4
(所影响的行数为 3 行)
*/
select id , max([max]) [max] from
(
select id , now [max] from table1
union all
select id , now [Now] from table1
union all
select id , Future [max] from table1
) t
group by id
/*
id Max
----------- -----------
1 5
2 3
3 4
(所影响的行数为 3 行)
*/
drop table table1