日期:2014-05-17 浏览次数:20641 次
create table testmax(city varchar(100),date datetime,num1 float,num2 float,num3 float,num4 float)
insert into testmax values('广州市','2013-10-01',1.3,45.0,2.4,30.05)
insert into testmax values('广州市','2013-10-02',1,2,3,4)
insert into testmax values('广州市','2013-10-03',140.3,45.0,2.4,30.05)
insert into testmax values('广州市','2013-10-04',1.3,45.0,9,30.05)
create table testmax(city varchar(100),date datetime,num1 float,num2 float,num3 float,num4 float)
insert into testmax values('广州市','2013-10-01',1.3,45.0,2.4,30.05)
insert into testmax values('广州市','2013-10-02',1,2,3,4)
insert into testmax values('广州市','2013-10-03',140.3,45.0,2.4,30.05)
insert into testmax values('广州市','2013-10-04',1.3,45.0,9,30.05)
select city,[date],max_num=case when max_num1>max_num2 then max_num1 else max_num2 end
from
(select city,[date],max_num1=case when num1>num2 then num1 else num2 end
,max_num2=case when num3>num4 then num3 else num4 end
from testmax
)t
/*
city date max_num
广州市 2013-10-01 00:00:00.000 45
广州市 2013-10-02 00:00:00.000 4
广州市 2013-10-03 00:00:00.000 140.3
广州市 2013-10-04 00:00:00.000 45
*/