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.
Author |
Topic |
mynameisfelix
Starting Member
7 Posts |
Posted - 2011-04-26 : 15:34:15
|
Hi,I have a table structure like thisCustomerID LabelID LabelValue100 CustomerCode ABC100 CustomerPin XYZ100 CustomerPass PQR200 CustomerCode IOP200 CustomerPin AIT200 CustomerPass PPPand I want the results to look like this. Can some one please help me with the query for this?CustomerID CustomerCode CustomerPin CustomerPass 100 ABC XYZ PQR200 IOP AIT PPPThanks is Advance |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-04-26 : 15:38:28
|
[code]DECLARE @t TABLE(CustomerID INT, LabelID VARCHAR(20), LabelValue VARCHAR(10))INSERT @t SELECT 100,'CustomerCode','ABC' UNION ALL SELECT 100,'CustomerPin','XYZ' UNION ALL SELECT 100,'CustomerPass','PQR' UNION ALL SELECT 200,'CustomerCode','IOP' UNION ALL SELECT 200,'CustomerPin','AIT' UNION ALL SELECT 200,'CustomerPass','PPP'SELECT CustomerID, CustomerCode, CustomerPin, CustomerPassFROM @tPIVOT(MAX(LabelValue) FOR LabelID IN(CustomerCode, CustomerPin, CustomerPass)) b[/code] |
 |
|
X002548
Not Just a Number
15586 Posts |
|
mynameisfelix
Starting Member
7 Posts |
Posted - 2011-04-27 : 11:58:38
|
Thank you, that perfectly solved my problem. What if there is a primary key column in the table like this. How would the pivot query look like in this case? ID CustomerID LabelID LabelValue1 100 CustomerCode ABC2 100 CustomerPin XYZ3 100 CustomerPass PQR4 200 CustomerCode IOP5 200 CustomerPin AIT6 200 CustomerPass PPP |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-04-27 : 12:04:02
|
If you include it in the SELECT clause then you won't get any pivoting, there's an implicit GROUP BY on unpivoted columns. You'll basically get the regular table back, and only one of the pivoted columns will contain data. Try it and you'll see what happens. |
 |
|
mynameisfelix
Starting Member
7 Posts |
Posted - 2011-04-27 : 12:15:15
|
Thanks for the reply. This is what happened and this is the query that I am using nowDECLARE @t TABLE(ID int,CustomerID INT, LabelID VARCHAR(20), LabelValue VARCHAR(10))INSERT @t SELECT 1, 100,'CustomerCode','ABC' UNION ALL SELECT 2, 100,'CustomerPin','XYZ' UNION ALL SELECT 3, 100,'CustomerPass','PQR' UNION ALL SELECT 4, 200,'CustomerCode','IOP' UNION ALL SELECT 5, 200,'CustomerPin','AIT' UNION ALL SELECT 6, 200,'CustomerPass','PPP'SELECT CustomerID, CustomerCode, CustomerPin, CustomerPassFROM @tPIVOT(MAX(LabelValue) FOR LabelID IN(CustomerCode, CustomerPin, CustomerPass)) bI am not using ID column in the select clause but these are the results that I am getting back nowCustomerID CustomerCode CustomerPin CustomerPass----------- ------------ ----------- ------------100 ABC NULL NULL100 NULL XYZ NULL100 NULL NULL PQR200 IOP NULL NULL200 NULL AIT NULL200 NULL NULL PPP |
 |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-04-27 : 12:18:23
|
[code]SELECT CustomerID, CustomerCode, CustomerPin, CustomerPassFROM (SELECT CustomerID, LabelID, LabelValue FROM @t) aPIVOT(MAX(LabelValue) FOR LabelID IN(CustomerCode, CustomerPin, CustomerPass)) b[/code] |
 |
|
mynameisfelix
Starting Member
7 Posts |
Posted - 2011-04-27 : 12:22:01
|
That perfectly worked. Thank you so much. |
 |
|
|
|
|
|
|