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 2005 Forums
 Transact-SQL (2005)
 Maximum on record level

Author  Topic 

GVeers
Starting Member

9 Posts

Posted - 2010-03-12 : 16:07:48
Say I have data 3 fields (columns) with the same data type. Is there an easy way to create a new field that has the maximum value of these 3 fields? It seems simple enough but I cannot for the life of me figure out how to do this with an UPDATE statement.

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2010-03-12 : 16:15:11
[CODE]update mytable
set field4 = case
when field1 >= field2 and field1 >= field3 then field1
when field2 >= field1 and field2 >= field3 then field2
else field3
end[/CODE]

=======================================
There are no passengers on spaceship earth. We are all crew. -Marshall McLuhan, educator and philosopher (1911-1980)
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-03-13 : 10:53:33
What do you mean when you say "create a new field"?
Would you please come up with an example?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-15 : 03:04:47
quote:
Originally posted by webfred

What do you mean when you say "create a new field"?
Would you please come up with an example?


No, you're never too old to Yak'n'Roll if you're too young to die.


I think it is a computed column

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

GVeers
Starting Member

9 Posts

Posted - 2010-03-15 : 09:25:55
madhivanan, as usual, speaks the truth. :)

Thanks Bustaz - I was trying to avoid a multiple if-then statement since it seems so clunky. But if that's the way to do it, then that's the way. I appreciate it!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-15 : 10:25:20
quote:
Originally posted by GVeers

madhivanan, as usual, speaks the truth. :)

Thanks Bustaz - I was trying to avoid a multiple if-then statement since it seems so clunky. But if that's the way to do it, then that's the way. I appreciate it!


another way is there which is by unpivoting and getting them to rows and then finding max and pivoting them back. but if the number of columns involved is small i prefer the suggested case when approach

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -