Solved

Convert rows to columns

Posted on 2011-02-22
4
835 Views
Last Modified: 2012-06-27
Hi,

I have a SP that produces this output:

YearWeek Shift TotalTime
--------- ------ -----------
2011/11   <NULL> 600        
2011/11   1      14875      
2011/11   2      2430        
2011/11   3      1445        
2011/11   4      12540      
2011/12   1      14540      
2011/12   2      2165        
2011/12   3      1580        
2011/12   4      9110        
2011/13   1      15455      
2011/13   2      2430        
2011/13   3      1580        
2011/13   4      17810      
2011/14   1      11385      
2011/14   2      1955        
2011/14   3      1520        
2011/14   4      14840      
...


Now I want to put the weeks in columns, like this:

Shift       2011/11  2011/12  2011/13  2011/14  2011/15 ...
<NULL>       600                          ...
1                14875    14540         ...
2                 2430      2165          ...
3                 1445      1580          ...
4                12540     9110          ...

I don't know how many colums I will have.

Any help? I'm trying some solutions that I have found on web but with no luck.

Thanks
Jorge Paulino
0
Comment
Question by:jpaulino
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 34950592
HI,

You can use Pivot for this purpose. Please read http://msdn.microsoft.com/en-us/library/ms177410(v=SQL.90).aspx for details.

Asim
0
 
LVL 48

Author Comment

by:jpaulino
ID: 34950614
Thanks for the quick reply, but the problem is that I don't know how many columns I will have.  
0
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 250 total points
ID: 34950644
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 250 total points
ID: 34951084
Hi jpaulino,

There are a couple of articles on the DB2 forum that describe recursive SQL and how to use it for converting row to columns, or columns to rows.  Except for DB2 using the double-pipe (||) and SQL Server using plus (+) for concatenation, the syntax is the same for DB2 and SQL Server.

The technique will work for you if you can't use pivot tables.

  http://www.experts-exchange.com/Database/DB2/A_3618-Recursive-SQL-in-DB2-Converting-rows-to-columns.html

  http://www.experts-exchange.com/Database/DB2/A_3629-Recursive-SQL-in-DB2-Converting-Columns-to-Rows.html


Good Luck,
Kent
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now