Log in  ::  Register  ::  Not logged in

## Convert Rows into Columns

 Author Message mpdillon SSCommitted Group: General Forum Members Points: 1929 Visits: 406 Comments posted to this topic are about the item Convert Rows into Columns kevriley SSCertifiable Group: General Forum Members Points: 6394 Visits: 2653 Nice.A simpler (IMHO) way of determining the ROWID is to simply subtract the Remainder from the RowNumber, a technique used a lot in "Gaps and Islands" solutions.`select max(case when remainder = 0 then LotNo else '' end), max(case when remainder = 1 then LotNo else '' end), max(case when remainder = 2 then LotNo else '' end)from( select LotNo , ( row_number() over ( order by LotNo ) +2 ) % 3 as Remainder , row_number() over ( order by LotNo ) - ( row_number() over ( order by LotNo ) +2 ) % 3 as ROWID from lotno)agroup by ROWID ` Steph Locke Ten Centuries Group: General Forum Members Points: 1155 Visits: 870 This seems really useful but the article formatting could do with a bit of improving - not many of codeblocks are correctly formatted Clif.Johnson-989960 Forum Newbie Group: General Forum Members Points: 5 Visits: 276 Here's a shorter version. Hope it helps. `SELECT [0] Col1,[1] Col2,[2] Col3FROM ( SELECT LotNo , ((ROW_NUMBER() OVER (ORDER BY LotNo)-1) % 3) col , ((ROW_NUMBER() OVER (ORDER BY LotNo)-1) / 3) roww FROM (VALUES('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H'),('I'),('J'),('K'),('L'),('M'),('N'),('O'),('P'),('Q'),('R'),('S'),('T'),('U'),('V'),('W'),('X'),('Y'),('Z'))t(LotNo) ) x PIVOT (MIN(LotNo) FOR col IN ([0],[1],[2])) p`(2008 required for VALUES) germany000 Forum Newbie Group: General Forum Members Points: 4 Visits: 6 Wouldn't it be easier to use PIVOT and UNPIVOT to transpose the columns?http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx Luis Cazares SSC Guru Group: General Forum Members Points: 95089 Visits: 21186 I once had a similar requirement and the best option was to generate the columns in the reporting tool. That was on VFP and I'm not sure if it's possible to do it in every reporting tool available, but it would certainly be easier to maintain and understand (single column from recordset printed in several columns on the report).However, if someone needs to do it in T-SQL, it's a good option, but certainly the CASE statement is not necessary.`WITH MstrTable AS( SELECT LotNo , ( ROW_NUMBER() OVER ( ORDER BY LotNo ) + 2 ) % 3 as Remainder , ( ROW_NUMBER() OVER ( ORDER BY LotNo ) + 2 ) / 3 as RowID FROM LotNo)SELECT MAX ( CASE WHEN remainder = 0 THEN LotNo ELSE '' END ) as Col1 ,MAX ( CASE WHEN remainder = 1 THEN LotNo ELSE '' END ) as Col2 ,MAX ( CASE WHEN remainder = 2 THEN LotNo ELSE '' END ) as Col3FROM mstrtableGROUP BY ROWID;--Second option just for fun, but shouldn't be better and might be worse.WITH Groups AS( SELECT LotNo , NTILE( (SELECT COUNT(*) FROM LotNo) / 3 + 1) OVER(ORDER BY LotNo) as RowID FROM LotNo),mstrtable AS( SELECT LotNo, RowID, ROW_NUMBER() OVER( PARTITION BY RowID ORDER BY LotNo) AS remainder FROM Groups)SELECT MAX ( CASE WHEN remainder = 1 THEN LotNo ELSE '' END ) as Col1 ,MAX ( CASE WHEN remainder = 2 THEN LotNo ELSE '' END ) as Col2 ,MAX ( CASE WHEN remainder = 3 THEN LotNo ELSE '' END ) as Col3FROM mstrtableGROUP BY ROWID;` Luis C.General Disclaimer:Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?How to post data/code on a forum to get the best help: Option 1 / Option 2 Robert.Sterbal Ten Centuries Group: General Forum Members Points: 1175 Visits: 2000 Looks like a great technique, although more than half the sites I support are SQL Server 2KI'll have to figure out how to do this in excel (with the so I can take a column and spread it into 3.Thanks! Luis Cazares SSC Guru Group: General Forum Members Points: 95089 Visits: 21186 Robert.Sterbal (7/17/2014)Looks like a great technique, although more than half the sites I support are SQL Server 2KI'll have to figure out how to do this in excel (with the so I can take a column and spread it into 3.Thanks!Hey Robert,For 2000, you could use a temp table with a new identity to ensure the gaps are not a problem.Here's an example using the same data as the article.`SELECT IDENTITY( int, 3, 1) AS RowNo, --Note the Identity definition starts at 3 LotNoINTO #LotNoFROM LotNoSELECT MAX( CASE WHEN RowNo % 3 = 0 THEN LotNo ELSE '' END) AS Column1, MAX( CASE WHEN RowNo % 3 = 1 THEN LotNo ELSE '' END) AS Column2, MAX( CASE WHEN RowNo % 3 = 2 THEN LotNo ELSE '' END) AS Column3FROM #LotNoGROUP BY RowNo / 3DROP TABLE #LotNo` Luis C.General Disclaimer:Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?How to post data/code on a forum to get the best help: Option 1 / Option 2 ScottPletcher SSC-Forever Group: General Forum Members Points: 46233 Visits: 8027 You really need only a single ROW_NUMBER function:`SELECT MAX(CASE WHEN row_num % 3 = 0 THEN LotNo ELSE '' END) AS Lot1, MAX(CASE WHEN row_num % 3 = 1 THEN LotNo ELSE '' END) AS Lot2, MAX(CASE WHEN row_num % 3 = 2 THEN LotNo ELSE '' END) AS Lot3 FROM ( SELECT LotNo, ROW_NUMBER() OVER(ORDER BY LotNo) + 2 AS row_num FROM LotNo) AS derivedGROUP BY row_num / 3` SQL DBA,SQL Server MVP(07, 08, 09) Prosecutor James Blackburn, in closing argument in the Fatal Vision murders trial: If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them. sgross 10581 SSC Rookie Group: General Forum Members Points: 36 Visits: 13 Is it possible to make this dynamic to discover the needed columns without Dynamic SQL?

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2017      SQL Server 2017 - Administration      SQL Server 2017 - Development SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 15 - Administration      SQL Server 15 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions      CosmosDB      Azure Data Lake      Azure Machine Learning Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization      Security and Auditing SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      SQL Server Security Skills      Question of the Day (QOD) Microsoft Access      Microsoft Access Products and Books      Third Party Products         SQLCentric         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         Golden Gate Software         Idera         Lumigent         Red Gate Software         ApexSQL         Quest Software         CA      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 2000 Books         Discuss SQL Server 7.0 Books Notification Services      Administration Article Discussions Future Versions      SQL 12

## Search

 Copyright © 2002-2018 Redgate. All Rights Reserved. Privacy Policy. Terms of Use. Report Abuse.