Is there a way to find the greatest value for any numeric column on the whole of a table?

advertisements

Is there any way to find the largest value for any column or any data type in a table?

Let me eleaborate on this a bit more. If i have a table with 10 columns and some columns have datatype int, and some have decimal data type. Is there any sql function to get the largest 'numerical' value from that table?

P_ID  Price Quantity CreateDate                 UpdateDate
1      5.99   60     2014-09-02 07:06:16.000    2014-09-02 07:06:16.000
2      45     20     2014-09-02 07:06:16.000    2014-09-02 07:06:16.000
3      29.99  35     2014-09-02 07:06:16.000    2014-09-02 07:06:16.000
4      95     10     2014-09-02 07:06:16.000    2014-09-02 07:06:16.000
5      199    20     2014-09-02 07:06:16.000    2014-09-02 07:06:16.000

EDIT: The example i have given is just an illustration and not actual data. In the example i want to be able to see '199' as the result. I know that MAX(Price) will give me 199 but i cannot assume this in my actual table. So i need something that will give me the max values for about 50000 records... I need something generic if possible that can work on any table...


Try this, unfortunately you have to list all the columns. Would be easier if they were same type:

SELECT max(val)
FROM
(SELECT CAST(p_id as decimal(11,2)) p_id,
 CAST(Price as decimal(11,2)) Price,
 CAST(Quantity as decimal(11,2)) Quantity
FROM yourtable) as p
UNPIVOT
(val FOR Seq IN
([p_id], [Price], [Quantity]) ) AS unpvt