471,621 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,621 software developers and data experts.

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 2185
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 XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.