How do I convert column data into row data?

 Author Message gregorykearney SSC Journeyman Group: General Forum Members Points: 84 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? PiMané SSCertifiable Group: General Forum Members Points: 7109 Visits: 1364 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... PiMané SSCertifiable Group: General Forum Members Points: 7109 Visits: 1364 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... ChrisM@Work SSC Guru Group: General Forum Members Points: 96451 Visits: 20667 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 PiMané SSCertifiable Group: General Forum Members Points: 7109 Visits: 1364 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... ChrisM@Work SSC Guru Group: General Forum Members Points: 96451 Visits: 20667 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 gregorykearney SSC Journeyman Group: General Forum Members Points: 84 Visits: 41 Great working examples, thanks for the education! dwain.c SSC-Forever Group: General Forum Members Points: 44007 Visits: 6431 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 ChrisM@Work SSC Guru Group: General Forum Members Points: 96451 Visits: 20667 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 Erland Sommarskog SSChampion Group: General Forum Members Points: 13236 Visits: 879 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