Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pivot only certain columns

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-09-16 : 04:34:03
Hi all

not exactly sure what I am supposed to do here if it is pivot or unpivot. I have a data set where all columns after the first column need to pivot but the first column then needs to relate the value it has against the original and show the duplicate value, as shown below if you don't understand the requirement:


Number Column1 Column2 Column3
====== ======= ======= =======
1 20 60 30
2 12 8
3 5 60 30
4 16 67



To


Number New Col1 Value
====== ======= =======
1 column1 20
1 column2 60
1 column3 30
2 column1
2 colunm2 12
2 column3 8
...
...
...


Any thoughts on the steps I need to take would be most helpful.

G

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2011-09-16 : 06:14:09
I think it was unpivot I had to use.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-16 : 12:20:21
its indeed unpivot

SELECT Number,[New Col],[Value]
FROM (SELECT Number,
ISNULL(Column1,'') AS Column1,
ISNULL(Column2,'') AS Column2,
ISNULL(Column3,'') AS Column3
FROM Table
)t
UNPIVOT([Value] FOR [New Col1] IN ([Column1],[Column2],[Column3]))u


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -