Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get Max value of (Col1,Col2,Col3) in SQL Server 2008

SQL
ID C1 C2  C3   Result(Max)
T1 1, 3 , 5    5
T2 5, 1,  10   10
T3 8, 15, 2    15



I need Result(Max)
Posted
Updated 11-Feb-13 19:08pm
v2

1 solution

You can do this using CASE (Transact-SQL)[^].
Here is a sample
SQL
DECLARE @T TABLE
(
	ID VARCHAR(10),
	C1 INT,
	C2 INT,
	C3 INT
)
INSERT INTO @T
SELECT 'T1', 1, 3 , 5 UNION ALL
SELECT 'T2', 5, 1, 10 UNION ALL
SELECT 'T3', 8, 15, 2

SELECT *, CASE WHEN C1 > C2 AND C1 > C3 THEN C1
			WHEN C2 > C1 AND C2 > C3 THEN C2
			ELSE C3 END AS [Result(Max)]
FROM @T
 
Share this answer
 
Comments
Aarti Meswania 12-Feb-13 1:09am    
5+ :)
__TR__ 12-Feb-13 1:31am    
Thank you.
sundar-indo 12-Feb-13 1:27am    
This is ok, but i need data pull from database.
(1, 3 , 5) * These all value coming from database.
Thanks for your solution.. Its almost ok, need some modification.
__TR__ 12-Feb-13 1:31am    
The query I wrote in my solution is just a sample. You can refer it and build your own query.
sundar-indo 12-Feb-13 3:26am    
Thanks...
I solved my problem...

SELECT *, CASE WHEN gws1 > gws2 AND gws1 > gws3 THEN gws1
WHEN gws2 > gws1 AND gws2 > gws3 THEN gws2
ELSE gws3 END AS [Result(Max)]
FROM wtmon2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900