Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a table with three columnst1 t2 t3 2 5 3I want to find out maximum of these three columns using sql query.so plz help me.
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2006-08-03 : 00:46:33
[code]declare @t table( t1 int, t2 int, t3 int)insert into @tselect 2, 5, 3 union allselect 1, 2, 3 union allselect 1, 3, 2 union allselect 3, 2, 1select *, max_of_all = case when t1 > t2 then case when t1 > t3 then t1 else t3 end else case when t2 > t3 then t2 else t3 end endfrom @tt1 t2 t3 max_of_all ----------- ----------- ----------- ----------- 2 5 3 51 2 3 31 3 2 33 2 1 3[/code]KH
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2006-08-03 : 00:49:53
Another way
declare @t table( t1 int, t2 int, t3 int)insert into @tselect 2, 5, 3 union allselect 1, 2, 3 union allselect 1, 3, 2 union allselect 3, 2, 1select *, ( select max(t) from (select t1 as t union all select t2 as t union all select t3) m ) as max_of_allfrom @t