SQL MAX VALUE.

Hello, Does anyone know if it is possible to get the max value from  a select statement that is comparing 9 columns?  Below are the columns I am pulling..

select thckpspermove,thckmidpermove,thckoppermove,flowpsidemove,flowmidpermove,flowopsidepermove, widthpspermove,widthmidpermove,widthopspermove
from  ENS_DIMENSIONALSTAB

The columns are all decimal values.  I was hoping there was a simple way of comparing.

Thanks
stevendeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
So ... the max of those nine columns?   Give this a whirl..
SELECT *,  (
   SELECT MAX(val) FROM (
      VALUES (thckpspermove), (thckmidpermove),  (thckoppermove), 
      (flowpsidemove), (flowmidpermove), (flowopsidepermove), 
      (widthpspermove),(widthmidpermove),(widthopspermove)
       ) AS value(val)) AS MaxVal 
FROM ENS_DIMENSIONALSTAB

Open in new window

0
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT MAX (Value) from (
select thckpspermove  value from  ENS_DIMENSIONALSTAB union all
select thckmidpermove from  ENS_DIMENSIONALSTAB union all
select thckoppermove from  ENS_DIMENSIONALSTAB union all
select flowpsidemove  from  ENS_DIMENSIONALSTAB union all
select flowmidpermove from  ENS_DIMENSIONALSTAB union all
select flowopsidepermove from  ENS_DIMENSIONALSTAB union all
select widthpspermove from  ENS_DIMENSIONALSTAB union all
select widthmidpermove from  ENS_DIMENSIONALSTAB union all
select widthopspermove from  ENS_DIMENSIONALSTAB

)A
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You can also copy-paste the below into your SSMS, execute it to verify it works, then modify it to fit your situation
CREATE TABLE #foo (c1 int, c2 int, c3 int) 

INSERT INTO #foo (c1, c2, c3) 
VALUES (5,7,9),(55,44,32),(1,999,7)

SELECT *,      
   (SELECT MAX(val) FROM (VALUES (c1), (c2), (c3)) AS value(val)) AS MaxVal 
FROM #foo

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

stevendeveloperAuthor Commented:
Error message...


Incorrect syntax near the keyword 'VALUES'.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Looks like VALUES was introduced in SQL 2008 R2, and looking at your tags it looks like you're in 2005.
So .. you'll probably have to go with aneesh's solution.
0
awking00Information Technology SpecialistCommented:
Are you looking for the max value for each column in one row or for the max value found in columns 1 thru 9 for each row?
0
stevendeveloperAuthor Commented:
The max value from all columns, Aneesh's solution looks good so far.
0
PortletPaulfreelancerCommented:
Not sure if this will be helpful now, but CROSS APPLY can be used here (and this approach should work in sql 2005).
I set-up those 9 columns in Excel produced some random decimals and then added an extra column 'expected' being the highest value per row.  I'm assuming you wanted a maximum - per row - and I've assumed a field like ID exists for that.
    **Query 1**:

    SELECT
           ID
         , max(ca1.stacked) AS max_value_per_row
         , expect
    FROM ENS_DIMENSIONALSTAB
    CROSS APPLY (
                    SELECT thckpspermove     UNION ALL
                    SELECT thckmidpermove    UNION ALL
                    SELECT thckoppermove     UNION ALL
                    SELECT flowpsidemove     UNION ALL
                    SELECT flowmidpermove    UNION ALL
                    SELECT flowopsidepermove UNION ALL
                    SELECT widthpspermove    UNION ALL
                    SELECT widthmidpermove   UNION ALL
                    SELECT widthopspermove
                ) AS ca1 (stacked)
    GROUP BY
           ID
         , expect
;    	
**[Results][2]**:
    
    | ID | MAX_VALUE_PER_ROW | EXPECT |
    |----|-------------------|--------|
    |  1 |             62.69 |  62.69 |
    |  2 |            119.35 | 119.35 |
    |  3 |            119.88 | 119.88 |
    |  4 |            123.11 | 123.11 |
    |  5 |            104.12 | 104.12 |
    |  6 |            119.21 | 119.21 |
    |  7 |            120.02 | 120.02 |
    |  8 |            114.01 | 114.01 |
    |  9 |            117.77 | 117.77 |
    | 10 |            109.98 | 109.98 |
    | 11 |            113.52 | 113.52 |
    | 12 |            102.66 | 102.66 |
    | 13 |            121.23 | 121.23 |
    | 14 |            108.85 | 108.85 |
    | 15 |            119.54 | 119.54 |
    | 16 |            103.76 | 103.76 |
    | 17 |            113.08 | 113.08 |
    | 18 |            111.69 | 111.69 |
    | 19 |            123.25 | 123.25 |
    | 20 |             101.1 |  101.1 |
    | 21 |            117.84 | 117.84 |
    | 22 |            107.83 | 107.83 |
    | 23 |            119.75 | 119.75 |
    | 24 |             112.8 |  112.8 |
    | 25 |             117.9 |  117.9 |
    | 26 |            115.08 | 115.08 |
    | 27 |            114.83 | 114.83 |
    | 28 |            123.54 | 123.54 |
    | 29 |            120.85 | 120.85 |
    | 30 |            122.06 | 122.06 |



  [1]: http://sqlfiddle.com/#!3/822c6/3

  [2]: http://sqlfiddle.com/#!3/822c6/3/0

===============================================
===============================================

    CREATE TABLE ENS_DIMENSIONALSTAB
    	(  ID int identity primary key,
          [thckpspermove] decimal(8,2), [thckmidpermove] decimal(8,2), [thckoppermove] decimal(8,2), [flowpsidemove] decimal(8,2), [flowmidpermove] decimal(8,2), [flowopsidepermove] decimal(8,2), [widthpspermove] decimal(8,2), [widthmidpermove] decimal(8,2), [widthopspermove] decimal(8,2), [Expect] decimal(8,2))
    ;
    	
    INSERT INTO ENS_DIMENSIONALSTAB
    	([thckpspermove], [thckmidpermove], [thckoppermove], [flowpsidemove], [flowmidpermove], [flowopsidepermove], [widthpspermove], [widthmidpermove], [widthopspermove], [Expect])
    VALUES
    	(3.21, 62.69, 56.03, 51.66, 56.80, 31.96, 30.39, 21.76, 2.51, 62.69),
    	(71.61, 101.46, 39.51, 118.76, 111.36, 81.94, 22.08, 115.77, 119.35, 119.35),
    	(91.73, 26.11, 54.48, 80.88, 119.88, 54.98, 1.75, 38.61, 98.13, 119.88),
    	(2.81, 94.46, 17.73, 123.11, 76.18, 121.17, 63.51, 13.05, 12.86, 123.11),
    	(61.75, 74.42, 71.41, 47.03, 36.76, 40.92, 31.25, 104.12, 40.95, 104.12),
    	(8.97, 119.21, 63.48, 15.12, 111.47, 6.11, 117.05, 48.12, 4.78, 119.21),
    	(56.49, 50.69, 40.12, 101.55, 88.71, 120.02, 19.99, 31.99, 60.68, 120.02),
    	(69.37, 110.11, 86.61, 52.26, 62.29, 9.58, 92.06, 7.57, 114.01, 114.01),
    	(106.04, 117.77, 40.59, 38.22, 71.43, 27.80, 8.68, 41.16, 62.01, 117.77),
    	(44.29, 33.14, 28.82, 34.22, 108.53, 66.22, 109.98, 9.80, 16.39, 109.98),
    	(77.10, 66.29, 54.86, 113.52, 95.11, 62.62, 36.06, 18.79, 83.10, 113.52),
    	(102.66, 68.60, 5.40, 58.64, 41.46, 7.48, 18.20, 102.07, 36.40, 102.66),
    	(55.41, 60.59, 55.62, 69.96, 98.22, 59.59, 121.23, 19.45, 20.30, 121.23),
    	(24.73, 24.58, 52.54, 8.30, 5.05, 108.85, 88.56, 32.50, 68.31, 108.85),
    	(73.54, 55.45, 26.71, 96.53, 6.08, 119.54, 88.86, 1.34, 76.21, 119.54),
    	(92.30, 53.29, 62.58, 103.76, 5.07, 43.95, 13.49, 29.15, 1.52, 103.76),
    	(68.36, 28.13, 113.08, 18.55, 109.55, 66.71, 39.22, 112.76, 78.95, 113.08),
    	(104.42, 67.71, 42.25, 61.30, 58.96, 37.64, 76.16, 111.69, 64.66, 111.69),
    	(2.86, 35.47, 84.59, 119.16, 94.33, 119.36, 41.32, 114.14, 123.25, 123.25),
    	(96.09, 36.72, 96.86, 101.10, 73.87, 79.26, 24.06, 66.11, 1.48, 101.10),
    	(75.79, 117.84, 15.91, 55.59, 78.03, 13.11, 7.39, 52.77, 78.49, 117.84),
    	(53.07, 25.60, 23.32, 80.82, 90.53, 71.93, 57.99, 83.76, 107.83, 107.83),
    	(63.18, 102.07, 34.02, 65.39, 115.26, 9.19, 2.09, 119.75, 100.69, 119.75),
    	(44.99, 82.12, 102.22, 112.80, 26.86, 43.81, 52.02, 30.40, 111.32, 112.80),
    	(117.01, 50.77, 13.39, 68.68, 39.38, 12.42, 117.90, 59.81, 70.35, 117.90),
    	(13.68, 35.32, 89.78, 115.08, 5.05, 24.61, 11.59, 109.19, 40.45, 115.08),
    	(97.73, 41.03, 114.83, 44.92, 90.82, 112.68, 60.79, 51.86, 93.50, 114.83),
    	(18.38, 18.83, 27.53, 25.93, 46.64, 123.54, 97.29, 93.26, 22.19, 123.54),
    	(2.08, 84.22, 94.44, 53.22, 46.34, 50.29, 120.85, 66.87, 117.33, 120.85),
    	(64.17, 68.23, 67.89, 23.16, 85.29, 75.64, 14.42, 122.06, 44.40, 122.06)
    ;

Open in new window

0
stevendeveloperAuthor Commented:
I like this,  I will try and let you know..Thanks to all for the help for sure!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.