Here is a sample table
DECLARE @t TABLE(a INT,b INT,c INT);
INSERT @t VALUES(1,2,3),(9,8,7),(4,6,5);
SELECT *
, ( SELECT MAX(val)
FROM (VALUES (a)
, (b)
, (c)
) AS value(val)
) AS MaxVal
FROM @t;
Result:
A B C MAX
---------------
1 2 3 3
9 8 7 9
4 6 5 6
I would like to add a column Max_cols which will have other column names as its values. column names are respective to the values that column 'MAX' has in it ...(hope this makes sense).
The result should look like this.
A B C MAX Max_cols
--------------------------
1 2 3 3 C
9 8 7 9 A
4 6 5 6 B