Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sql server 2008 - How to transpose data into a new table

Posted on 2013-05-14
2
Medium Priority
?
455 Views
Last Modified: 2013-05-15
Hi..
I have an odd table that looks like this

CUST_ID       ORDER1   ORDER2   ORDER3   ORDERDATE1    ORDERDATE2  ORDERDATE3
1         Shoes        Pants        Socks         1/2/2013         1/3/2013         1/4/2013

How can I create an a query that will put it into a new table that looks like this

CUST_ID     ORDER           ORDERDATE
1       Shores               1/2/2013
1       Pants                 1/3/2013
1      Socks                  1/4/2013



thanks
0
Comment
Question by:JElster
2 Comments
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 1000 total points
ID: 39166631
can you try this.
;with cte1 as (select cust_id,OrdersCol,Orders,row_number() over (partition by cust_id order by OrdersCol) rn 
                 from test 
			  unpivot (Orders for OrdersCol in (Order1,Order2,Order3)) unpvt),
      cte2 as (select cust_id,OrderDateCol,OrderDate,row_number() over (partition by cust_id order by OrderDateCol) rn 
	            from test 
			  unpivot (OrderDate for OrderDateCol in (OrderDate1,OrderDate2,OrderDate3)) unpvt)
select c1.cust_id,c1.Orders,c2.OrderDate
  from cte1 c1
  join cte2 c2 on c1.cust_id = c2.cust_id and c1.rn = c2.rn  

Open in new window

see here: http://sqlfiddle.com/#!3/3f264/7
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 1000 total points
ID: 39166884
I think a simpler approach is available, using cross apply:
select
  cust_id
, orderid
, orderdate
from test
cross apply (
        values
                 (ORDER1,ORDERDATE1)
               , (ORDER2,ORDERDATE2)
               , (ORDER3,ORDERDATE3)
             ) as crossapplied (ORDERID, ORDERDATE)  

Open in new window

http://sqlfiddle.com/#!3/3f264/10

nb: you might want to avoid calling a field 'order' as that is a sql reserved word 'orderid' or something along those lines might be a better option.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

886 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