sql:取最大值
需求:同一行数据取得最大值
测试的数据如下:
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