Click here to Skip to main content
15,921,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Please find the below sql server - total table.

Col 1 Col 2 Col 3 Col 4
1 5 6 8
11 14 18 12


i want to out put of maximum number in the total table.
Posted
Comments
CHill60 14-Mar-15 7:21am    
Do you mean the maximum value across all columns? What have you tried so far?
Raghu425 14-Mar-15 7:28am    
Hi,
yes i want maximum number acrross all columns,pls. help me on this.

Use UNPIVOT:
SQL
SELECT MAX(vx) FROM MyTable AS a
UNPIVOT (vx FOR Value IN (Col1, Col2, Col3, Col4)) AS up


[edit]Typo: missed "AS a"[/edit]
 
Share this answer
 
v2
Comments
Maciej Los 14-Mar-15 9:46am    
+5!
Solution1 by OriginalGriff[^] is very good. As an alternative, check this:

SQL
SELECT MAX(A.MyValue)
FROM (
    SELECT Col1 AS MyValue
    FROM TableName 
    UNION ALL
    SELECT Col2 AS MyValue
    FROM TableName 
    UNION ALL
    SELECT Col3 AS MyValue
    FROM TableName 
    UNION ALL
    SELECT Col4 AS MyValue
    FROM TableName 
) AS A
 
Share this answer
 
v2

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