| Author |
Topic |
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-24 : 09:11:34
|
| I would like the compare the different columns (forb) and fetch the one that has the highest value for each id.create table #ForbTable(id int,forb1 int null,forb2 int null,forb3 int null,forb4 int nullprimary key(id) )insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(1,10,2,1,20)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(2,3,13,12,1)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(3,7,10,55,5)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(91,90,10,12,21)Expected output:1 forb42 forb23 forb34 forb1Should I use a case?//Maevr |
|
|
binto
Yak Posting Veteran
59 Posts |
Posted - 2010-03-24 : 09:47:27
|
| can you please trySELECT test.id,test.ColumnName FROM ( SELECT id,forb1,forb2,forb3,forb4 FROM #ForbTable) t UNPIVOT (Colname FOR ColumnName IN (forb1,forb2,forb3,forb4) ) testThanks & RegardsBinto Thomas |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-24 : 10:06:26
|
| Thans for fast reply.The output for your select is:1 forb11 forb21 forb31 forb42 forb12 forb22 forb32 forb43 forb13 forb23 forb33 forb44 forb14 forb24 forb34 forb4 |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-03-24 : 11:12:24
|
quote: Originally posted by maevr I would like the compare the different columns (forb) and fetch the one that has the highest value for each id.create table #ForbTable(id int,forb1 int null,forb2 int null,forb3 int null,forb4 int nullprimary key(id) )insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(1,10,2,1,20)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(2,3,13,12,1)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(3,7,10,55,5)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(91,90,10,12,21)Expected output:1 forb42 forb23 forb34 forb1Should I use a case?//Maevr
How are you getting this expected output? What is the logic? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 11:24:58
|
| [code]SELECT id,ColumnNameFROM(SELECT test.id,test.ColumnName,Val,ROW_NUMBER() OVER (PARTITION BY id ORDER BY Val DESC) AS SeqFROM (SELECT id,forb1,forb2,forb3,forb4FROM #ForbTable) tUNPIVOT(Val FOR ColumnName IN (forb1,forb2,forb3,forb4)) test)rWHERE Seq=1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-25 : 02:45:17
|
| Thanks visakh16!I'm having trouble understanding your query, can you please explain? |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-25 : 03:47:44
|
| Another question.How can I get the value from the field that has the highest value returned in the same query?example:id column value1 forb4 202 forb2 133 forb3 554 forb1 90 |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-25 : 06:00:10
|
| I managed to solve the issue myself regarding my last post, thanks for all the help. |
 |
|
|
haroon2k9
Constraint Violating Yak Guru
328 Posts |
Posted - 2010-03-25 : 06:11:37
|
quote: Originally posted by maevr I managed to solve the issue myself regarding my last post, thanks for all the help.
could you please show us,How did you solve it? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-25 : 10:35:25
|
quote: Originally posted by maevr Thanks visakh16!I'm having trouble understanding your query, can you please explain?
i just added a rownumber function in your unpivoted query to number records for each id group based on the decreasing order of Val. This will cause records within each ID value group to be numbered 1,2,.. starting from largest value of Val. then retrieving values of records with row number as 1 will ensure you get always records with max value of Val within each ID group------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-03-30 : 01:56:49
|
| Hi again!What do I do if there is two (or more) with the same number?example:insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(5,20,2,1,20)Can I separate them using a '/' so that the response will be:5 forb1/forb4 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-30 : 13:12:01
|
quote: Originally posted by maevr Hi again!What do I do if there is two (or more) with the same number?example:insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(5,20,2,1,20)Can I separate them using a '/' so that the response will be:5 forb1/forb4
;With CTE(id,ColumnName,Val,Seq)AS(SELECT test.id,test.ColumnName,Val,DENSE_RANK() OVER (PARTITION BY id ORDER BY Val DESC) FROM (SELECT id,forb1,forb2,forb3,forb4FROM #ForbTable) tUNPIVOT(Val FOR ColumnName IN (forb1,forb2,forb3,forb4)) test)r)SELECT id,STUFF((SELECT '/'+ColumnName FROM CTE WHERE id=c.id AND Seq=1 FOR XML PATH('')),1,1,'') AS ValListFROM CTE cWHERE Seq=1------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
maevr
Posting Yak Master
169 Posts |
Posted - 2010-04-01 : 04:47:18
|
| Cannot get the last query to work.Copy/paste using the temporary table/data in the first post. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-01 : 05:01:26
|
| [code]create table #ForbTable(id int,forb1 int null,forb2 int null,forb3 int null,forb4 int nullprimary key(id) )insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(1,10,2,1,20)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(2,3,13,12,1)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(3,7,10,55,5)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(91,90,10,12,21)insert into #ForbTable(id,forb1,forb2,forb3,forb4) values(5,20,2,1,20);With CTE(id,ColumnName,Val,Seq)AS(SELECT test.id,test.ColumnName,Val,DENSE_RANK() OVER (PARTITION BY id ORDER BY Val DESC) FROM (SELECT id,forb1,forb2,forb3,forb4FROM #ForbTable) tUNPIVOT(Val FOR ColumnName IN (forb1,forb2,forb3,forb4)) test)SELECT DISTINCT id,STUFF((SELECT '/'+ColumnName FROM CTE WHERE id=c.id AND Seq=1 FOR XML PATH('')),1,1,'') AS ValListFROM CTE cWHERE Seq=1DROP TABLE #ForbTableoutput------------------------------id ValList1 forb42 forb23 forb35 forb1/forb491 forb1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|