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 3, 2013 9:23 PM
 Forum Newbie Group: General Forum Members Last Login: Friday, April 25, 2014 1:17 PM Points: 8, Visits: 41
 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 4, 2013 7:16 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Friday, November 18, 2016 2:29 PM Points: 592, Visits: 1,330
 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 4, 2013 7:19 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Friday, November 18, 2016 2:29 PM Points: 592, Visits: 1,330
 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 4, 2013 7:46 AM
 SSCrazy Eights Group: General Forum Members Last Login: Yesterday @ 9:41 AM Points: 8,605, Visits: 18,775
 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 4, 2013 8:01 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Friday, November 18, 2016 2:29 PM Points: 592, Visits: 1,330
 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 4, 2013 8:06 AM
 SSCrazy Eights Group: General Forum Members Last Login: Yesterday @ 9:41 AM Points: 8,605, Visits: 18,775
 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 4, 2013 2:50 PM
 Forum Newbie Group: General Forum Members Last Login: Friday, April 25, 2014 1:17 PM Points: 8, Visits: 41
 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: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 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!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1493537
 Posted Wednesday, September 11, 2013 4:00 AM
 SSCrazy Eights Group: General Forum Members Last Login: Yesterday @ 9:41 AM Points: 8,605, Visits: 18,775
 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
 SSC Eights! Group: General Forum Members Last Login: Wednesday, December 7, 2016 3:19 PM Points: 889, Visits: 863
 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