| Author |
Topic |
|
mavershang
Posting Yak Master
111 Posts |
Posted - 2009-02-19 : 18:14:10
|
| Hi all. My question is about to select the max value of multiple columns.For instance,the table is likecol1 col2 col3 col4 col5aa 1 2 4 6bb 8 7 1 3cc 11 3 1 10...The output I want is like-------------------aa 6bb 8cc 11....--------------------which is for each row, the max value of col2-col5 together with the value of col1.Thanks. |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 18:27:59
|
| [code]Select col1,Max(col2) from(Select col1,col2 from Tableunion allSelect col1,col3 from Tableunion allSelect col1,col4 from Tableunion allSelect col1,col5 from Table)ZGroup by col1Order by col1[/code] |
 |
|
|
Rajesh Jonnalagadda
Starting Member
45 Posts |
Posted - 2009-02-20 : 08:11:23
|
| Try this,CREATE TABLE #TEST(col1 NVARCHAR(50),col2 INT,col3 INT,col4 INT,col5 INT)INSERT INTO #TESTSELECT 'aa', 1, 2, 4, 6 UNION ALLSELECT 'bb', 8, 7, 1, 3 UNION ALLSELECT 'cc', 11, 3, 1, 10 SELECT col1, CASE WHEN col2 > col3 THEN col2 WHEN col3 > col4 THEN col3 WHEN col4 > col5 THEN col4 ELSE Col5 END AS ResultFROM #TESTRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url] |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-20 : 08:19:03
|
quote: Originally posted by Rajesh Jonnalagadda Try this,CREATE TABLE #TEST(col1 NVARCHAR(50),col2 INT,col3 INT,col4 INT,col5 INT)INSERT INTO #TESTSELECT 'aa', 1, 2, 4, 6 UNION ALLSELECT 'bb', 8, 7, 1, 3 UNION ALLSELECT 'cc', 11, 3, 1, 10 UNION ALLSELECT col1, CASE WHEN col2 > col3 THEN col2 WHEN col3 > col4 THEN col3 WHEN col4 > col5 THEN col4 ELSE Col5 END AS ResultFROM #TESTRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url]
Wouldn't work. |
 |
|
|
Rajesh Jonnalagadda
Starting Member
45 Posts |
Posted - 2009-02-20 : 08:33:31
|
| Hi sakets_2000,Thanks sakets.I have edited my post. check it nowRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url] |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-20 : 08:34:39
|
quote: Originally posted by Rajesh Jonnalagadda Hi sakets_2000,Thanks sakets.I have edited my post. check it nowRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url]
Try with this,INSERT INTO #TESTSELECT 'aa', 10, 2, 4, 600 UNION ALLSELECT 'bb', 800, 7, 1, 3000 UNION ALLSELECT 'cc', 11, 3, 1, 1000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-20 : 09:37:18
|
| [code]SELECT col1,MAX(Val)FROM(SELECT col1, col2, col3, col4, col5FROM Table)mUNPIVOT (Val FOR ColCat IN ([col2],[col3],[col4],[col5]))uGROUP BY col1[/code] |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 09:52:49
|
quote: Originally posted by sakets_2000
quote: Originally posted by Rajesh Jonnalagadda Try this,CREATE TABLE #TEST(col1 NVARCHAR(50),col2 INT,col3 INT,col4 INT,col5 INT)INSERT INTO #TESTSELECT 'aa', 1, 2, 4, 6 UNION ALLSELECT 'bb', 8, 7, 1, 3 UNION ALLSELECT 'cc', 11, 3, 1, 10 UNION ALLSELECT col1, CASE WHEN col2 > col3 THEN col2 WHEN col3 > col4 THEN col3 WHEN col4 > col5 THEN col4 ELSE Col5 END AS ResultFROM #TESTRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url]
Wouldn't work.
Try Like This:SELECT col1, Case When Col2>Col3 and Col2>Col4 and Col2>Col5 then Col2When Col3>Col2 and Col3>Col4 and Col3>Col5 then Col3When Col4>Col2 and Col4>Col3 and Col4>Col5 then Col4Else Col5 EndFROM #TEST |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-20 : 15:03:33
|
quote: Originally posted by sodeep
quote: Originally posted by sakets_2000
quote: Originally posted by Rajesh Jonnalagadda Try this,CREATE TABLE #TEST(col1 NVARCHAR(50),col2 INT,col3 INT,col4 INT,col5 INT)INSERT INTO #TESTSELECT 'aa', 1, 2, 4, 6 UNION ALLSELECT 'bb', 8, 7, 1, 3 UNION ALLSELECT 'cc', 11, 3, 1, 10 UNION ALLSELECT col1, CASE WHEN col2 > col3 THEN col2 WHEN col3 > col4 THEN col3 WHEN col4 > col5 THEN col4 ELSE Col5 END AS ResultFROM #TESTRajesh Jonnalagadda[url="http://www.ggktech.com"]GGK TECH[/url]
Wouldn't work.
Try Like This:SELECT col1, Case When Col2>Col3 and Col2>Col4 and Col2>Col5 then Col2When Col3>Col2 and Col3>Col4 and Col3>Col5 then Col3When Col4>Col2 and Col4>Col3 and Col4>Col5 then Col4Else Col5 EndFROM #TEST
You might want to test with this,INSERT INTO #TESTSELECT 'aa', 10, 10, 10, 2 UNION ALLSELECT 'bb', 10, 10, 10, 4 UNION ALLSELECT 'cc', 10, 10, 10, 6 |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 15:16:02
|
| Then change it like this:SELECT col1, Case When Col2>=Col3 and Col2>=Col4 and Col2>=Col5 then Col2When Col3>=Col2 and Col3>=Col4 and Col3>=Col5 then Col3When Col4>=Col2 and Col4>=Col3 and Col4>=Col5 then Col4Else Col5 EndFROM #TEST |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-02-20 : 15:26:26
|
quote: Originally posted by visakh16
SELECT col1,MAX(Val)FROM(SELECT col1, col2, col3, col4, col5FROM Table)mUNPIVOT (Val FOR ColCat IN ([col2],[col3],[col4],[col5]))uGROUP BY col1
I like Visakh's unpivot solution - but it could be slightly simpler:select col1, max(val)from #test dunpivot (val for Cols in ([col2],[col3],[col4],[col5])) ugroup by col1 Be One with the OptimizerTG |
 |
|
|
|
|
|