Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 How do I convert column data into row data? Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, September 03, 2013 9:23 PM
 Forum Newbie Group: General Forum Members Last Login: Monday, April 07, 2014 8:14 AM Points: 8, Visits: 40
 table gennumtollnum, n1, n2, n3800123, 1234, 1235, 1236999123, 9876, 9875, 9874I want to my data to look like:tollnum,code800123, 1234800123, 1235800123, 1236999123, 9876999123, 9875999123, 9874How do I convert column data into row data?
Post #1491128
 Posted Wednesday, September 04, 2013 7:16 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Tuesday, April 15, 2014 10:49 AM Points: 513, Visits: 1,123
 gregorykearney (9/3/2013)table gennumtollnum, n1, n2, n3800123, 1234, 1235, 1236999123, 9876, 9875, 9874I want to my data to look like:tollnum,code800123, 1234800123, 1235800123, 1236999123, 9876999123, 9875999123, 9874How do I convert column data into row data?Hi,You can use the UNPIVOT statment:SELECT tollnum, codeFROM (SELECT * FROM gennum) p UNPIVOT (code FOR n IN ( n1, n2, n3 ) ) AS unpvtORDER BY tollnum, n If you need to work better, try working less...
Post #1491279
 Posted Wednesday, September 04, 2013 7:19 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Tuesday, April 15, 2014 10:49 AM Points: 513, Visits: 1,123
 gregorykearney (9/3/2013)table gennumtollnum, n1, n2, n3800123, 1234, 1235, 1236999123, 9876, 9875, 9874I want to my data to look like:tollnum,code800123, 1234800123, 1235800123, 1236999123, 9876999123, 9875999123, 9874How do I convert column data into row data?Or a simple UNION ALLSELECT * FROM (SELECT tollnum, n1 as N FROM gennumUNION ALLSELECT tollnum, n2 FROM lixoUNION ALLSELECT tollnum, n3 FROM lixo) t ORDER BY tollnumEither way it works... If you need to work better, try working less...
Post #1491284
 Posted Wednesday, September 04, 2013 7:46 AM
 SSCertifiable Group: General Forum Members Last Login: Thursday, April 17, 2014 9:33 AM Points: 6,754, Visits: 12,854
 Or CROSS APPLY VALUES:`-- sample data;WITH gennum (tollnum, n1, n2, n3) AS ( SELECT 800123, 1234, 1235, 1236 UNION ALL SELECT 999123, 9876, 9875, 9874)-- solutionSELECT g.tollnum, d.code FROM gennum gCROSS APPLY (VALUES (n1), (n2), (n3)) d (code)` “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1491305
 Posted Wednesday, September 04, 2013 8:01 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Tuesday, April 15, 2014 10:49 AM Points: 513, Visits: 1,123
 ChrisM@Work (9/4/2013)Or CROSS APPLY VALUES:`-- sample data;WITH gennum (tollnum, n1, n2, n3) AS ( SELECT 800123, 1234, 1235, 1236 UNION ALL SELECT 999123, 9876, 9875, 9874)-- solutionSELECT g.tollnum, d.code FROM gennum gCROSS APPLY (VALUES (n1), (n2), (n3)) d (code)`Didn't know this one One more for the "bag" Nice. If you need to work better, try working less...
Post #1491323
 Posted Wednesday, September 04, 2013 8:06 AM
 SSCertifiable Group: General Forum Members Last Login: Thursday, April 17, 2014 9:33 AM Points: 6,754, Visits: 12,854
 PiMané (9/4/2013)ChrisM@Work (9/4/2013)Or CROSS APPLY VALUES:`-- sample data;WITH gennum (tollnum, n1, n2, n3) AS ( SELECT 800123, 1234, 1235, 1236 UNION ALL SELECT 999123, 9876, 9875, 9874)-- solutionSELECT g.tollnum, d.code FROM gennum gCROSS APPLY (VALUES (n1), (n2), (n3)) d (code)`Didn't know this one One more for the "bag" Nice.Dwain C has a nice article on it, Ped “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1491324
 Posted Wednesday, September 04, 2013 2:50 PM
 Forum Newbie Group: General Forum Members Last Login: Monday, April 07, 2014 8:14 AM Points: 8, Visits: 40
 Great working examples, thanks for the education!
Post #1491542
 Posted Wednesday, September 11, 2013 3:54 AM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 6:57 PM Points: 3,590, Visits: 5,099
 PiMané (9/4/2013)ChrisM@Work (9/4/2013)Or CROSS APPLY VALUES:`-- sample data;WITH gennum (tollnum, n1, n2, n3) AS ( SELECT 800123, 1234, 1235, 1236 UNION ALL SELECT 999123, 9876, 9875, 9874)-- solutionSELECT g.tollnum, d.code FROM gennum gCROSS APPLY (VALUES (n1), (n2), (n3)) d (code)`Didn't know this one One more for the "bag" Nice.And after all the work I've done trying to get the word out too! [face-to-palm]Thanks for the plug Chris! My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1493537
 Posted Wednesday, September 11, 2013 4:00 AM
 SSCertifiable Group: General Forum Members Last Login: Thursday, April 17, 2014 9:33 AM Points: 6,754, Visits: 12,854
 dwain.c (9/11/2013)PiMané (9/4/2013)ChrisM@Work (9/4/2013)Or CROSS APPLY VALUES:`-- sample data;WITH gennum (tollnum, n1, n2, n3) AS ( SELECT 800123, 1234, 1235, 1236 UNION ALL SELECT 999123, 9876, 9875, 9874)-- solutionSELECT g.tollnum, d.code FROM gennum gCROSS APPLY (VALUES (n1), (n2), (n3)) d (code)`Didn't know this one One more for the "bag" Nice.And after all the work I've done trying to get the word out too! [face-to-palm]Thanks for the plug Chris!I felt sorry for you tramping the streets of Port Moresby with that heavy sandwich board “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1493542
 Posted Wednesday, September 11, 2013 3:53 PM
 Right there with Babe Group: General Forum Members Last Login: Thursday, April 17, 2014 4:43 AM Points: 756, Visits: 631
 ChrisM@Work (9/4/2013)Or CROSS APPLY VALUESSomewhat odd to use CROSS APPLY here. The normal is CROSS JOIN. Of course since there is no correlation on the right side, the CROSS APPLU is effectively a CROSS JOIN, but nevertheless. Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1493931

 Permissions

 Copyright © 2002-2014 Simple Talk Publishing. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.