Solved

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

Posted on 2013-05-14
2
433 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 40

Assisted Solution

by:Sharath
Sharath earned 250 total points
Comment Utility
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 48

Accepted Solution

by:
PortletPaul earned 250 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

7 Experts available now in Live!

Get 1:1 Help Now