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 2000 Forums
 SQL Server Development (2000)
 plz help to solve query

Author  Topic 

sw_raviraj
Starting Member

1 Post

Posted - 2006-08-03 : 00:42:00
I have a table with three columns

t1 t2 t3
2 5 3

I 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 @t
select 2, 5, 3 union all
select 1, 2, 3 union all
select 1, 3, 2 union all
select 3, 2, 1


select *,
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
end
from @t

t1 t2 t3 max_of_all
----------- ----------- ----------- -----------
2 5 3 5
1 2 3 3
1 3 2 3
3 2 1 3
[/code]


KH

Go to Top of Page

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 @t
select 2, 5, 3 union all
select 1, 2, 3 union all
select 1, 3, 2 union all
select 3, 2, 1

select *,
(
select max(t) from (select t1 as t union all select t2 as t union all select t3) m
) as max_of_all
from @t



KH

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-08-03 : 11:05:28
Also read http://www.datamodel.org/NormalizationRules.html

Madhivanan

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

- Advertisement -