Solved

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

Posted on 2013-05-14
2
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 250 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 48

Accepted Solution

by:
PortletPaul earned 250 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach 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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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