Solved

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

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

827 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