469,106 Members | 2,139 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,106 developers. It's quick & easy.

Greatest value



Hi all. I'm looking for assistance to get the greatest value from 3 or
more different columns. I'm assuming that the best way is to put the
columns into a temp table and use 'max' function to return the greatest
value but don't know how to code it. Thanks in advance.

*** Sent via Developersdex http://www.developersdex.com ***
Mar 14 '06 #1
2 2148
Here's an example of what you want using a derived table
declare @t table(id int, val1 int,val2 int,val3 int)
insert into @t(id,val1,val2,val3) values(1,100,200,300)
insert into @t(id,val1,val2,val3) values(2,300,400,500)
insert into @t(id,val1,val2,val3) values(3,600,700,800)

select id,max(vals)
from
(
select id,val1
from @t
union all
select id,val2
from @t
union all
select id,val3
from @t
) X(id,vals)
group by id

Mar 14 '06 #2


*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by zaceti | last post: by
8 posts views Thread by Mike Nolan | last post: by
2 posts views Thread by Shark | last post: by
4 posts views Thread by tomek milewski | last post: by
2 posts views Thread by Shawn Minisall | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.