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
select city,[date],MAX(num) max_num from ( select city,[date],num1 num from testmax union all select city,[date],num2 num from testmax union all select city,[date],num3 num from testmax union all select city,[date],num4 num from testmax ) t group by city,[date]
有其他思路吗?
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)
go create function fn_getmax(@num1 float,@num2 float,@num3 float,@num4 float) returns float as begin return ( select MAX(num) from ( select @num1 as num union all select @num2 union all select @num3 union all select @num4 ) t ) end
select city ,[date],dbo.fn_getmax(num1,num2,num3,num4) as maxnum from testmax