Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 select max within row

Author  Topic 

Scott
Posting Yak Master

145 Posts

Posted - 2002-09-11 : 10:22:00
I have a table with 5 numeric fields. I want to select the field from each row with the max value for that row.

Is a nested case the best or is there a better way?

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2002-09-11 : 10:27:22
If the five fields are similar enough that you would want to compare them, then it sounds like your database design is not as normalised as it should be.

So, the best solution, from the sounds of it, is a redesign. Other than that, yeah, a nasty case statement is probably the way to go.

Damian

Edited by - Merkin on 09/11/2002 10:28:02
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-11 : 10:40:43
A derived table may be less-nasty way to do this ... but Merkin is right about the redesign ...

Jay White
{0}
Go to Top of Page

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-09-11 : 11:46:13
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) )
go

insert 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))) * 1000
from
#numbers -- (10000 rows)
go

select
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 maxnum
from
#scott
order by
pk

select
s.d.pk,
max(d.num) as maxnum
from
#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.pk
group by s.d.pk
order 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
Go to Top of Page

skond
Yak Posting Veteran

55 Posts

Posted - 2002-09-11 : 11:56:38
supposing your table is like this

key_field char(10),
num1 int,
num2 int,
num3 int,
num4 int,
num5 int

Write a select statement to flip the table into two columns and then get a max as follows:

select key_field,max(num) from
(
select key_field,num1 as num from table
union all
select key_field,num2 from table
union all
select key_field,num3 from table
union all
select key_field,num4 from table
union all
select key_field,num5 from table
) A
group by key_field



Go to Top of Page
   

- Advertisement -