Actually, it looks like the case statement is the way to go ...create table #scott ( pk int identity(1,1) not null primary key, num1 numeric(25,20), num2 numeric(25,20), num3 numeric(25,20), num4 numeric(25,20), num5 numeric(25,20) )goinsert into #scott (num1,num2,num3,num4,num5)select rand(cast(newid() as binary(4))) * 1000, rand(cast(newid() as binary(4))) * 1000, rand(cast(newid() as binary(4))) * 1000, rand(cast(newid() as binary(4))) * 1000, rand(cast(newid() as binary(4))) * 1000from #numbers -- (10000 rows)goselect pk, case when num1 >= num2 and num1 >= num3 and num1 >= num4 and num1 >= num5 then num1 when num2 >= num1 and num2 >= num3 and num2 >= num4 and num2 >= num5 then num2 when num3 >= num1 and num3 >= num2 and num3 >= num4 and num3 >= num5 then num3 when num4 >= num1 and num4 >= num2 and num4 >= num3 and num4 >= num5 then num4 else num5 end as maxnumfrom #scottorder by pkselect s.d.pk, max(d.num) as maxnumfrom #scott inner join ( select pk,num1 as num from #scott union all select pk,num2 as num from #scott union all select pk,num3 as num from #scott union all select pk,num4 as num from #scott union all select pk,num5 as num from #scott) d on s.pk = d.pkgroup by s.d.pkorder by s.d.pk
Execution of the first is several times faster ...Jay White{0}Edited by - Page47 on 09/11/2002 12:04:57