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)
 taking the column with the greater value

Author  Topic 

stonebreaker
Yak Posting Veteran

92 Posts

Posted - 2009-02-04 : 14:05:13
I am comparing work order hours in two different tables. Both tables have a column for number of labor hours required to complete the work order. I want to create a view that takes the greater value of the two columns and displays it.

Select column_a
,column_b
,<the greater of>(column_c or column_d)as labor_hours
from table_1 join table_2
on ...

Is there a command or function for this? Or will I have to write a case statement?

Thanks in advance,

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-04 : 14:43:48
no, You'll have to use case statement.
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-05 : 00:03:14
better to use case statement as sakets said

try ur query as
select column_1,column_b ,
case when column_c > column_d then column_c else column_d end as labor_hours
from table_1 join table_2
on..........
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-05 : 06:22:12
Or,You could make a function to find greater,median,avg,least etc if you need it very often.
Go to Top of Page

stonebreaker
Yak Posting Veteran

92 Posts

Posted - 2009-02-05 : 10:22:18
OK, thanks for the help. Oracle has a GREATEST function, I just wondered if there was an equivalent one in SQL Server.
Go to Top of Page
   

- Advertisement -