?
Solved

How to transform vertical data to horizontal format

Posted on 2008-10-19
4
Medium Priority
?
661 Views
Last Modified: 2010-04-21
I am trying transform vertical data into a single record horizontal layout.  I have tried a number of ways including pivot, crosstabs, etc.   Can anyone provide assstance?

Table1
VendorID      PymtVendor
COM35         COM35P
COM35         COM35Q
COM35         COM35S

Desired Results

VendorID          PymtVendor1         PymtVendor2          PymtVendor3
COM35              COM35P                 COM35Q                 COM35S

Please note there could be up to 10 Payment vendors per vendor ID.

Thank you in advance.

0
Comment
Question by:asimillc
[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
  • 2
4 Comments
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22752842
Any particular way you want them to appear?  As in, order?

This will sort by name so the first alphabetical will be 1, then 2..... pymtvendor .
;with paymentvendors as (select vendorid,pymtvendor,row_number() over (partition by vendorid order by pymtvendor) rn from table1)
select pv1.vendorid, pv1.pymtvendor, pv2.pymtvendor, pv3.pymtvendor, pv4.pymtvendor, pv5.pymtvendor, pv6.pymtvendor, pv7.pymtvendor, pv8.pymtvendor, pv9.pymtvendor, pv10.pymtvendor
from paymentvendors pv1
left outer join paymentvendors pv2 on pv1.vendorid = pv2.vendorid and pv2.rn=2
left outer join paymentvendors pv3 on pv1.vendorid = pv3.vendorid and pv3.rn=3
left outer join paymentvendors pv4 on pv1.vendorid = pv4.vendorid and pv4.rn=4
left outer join paymentvendors pv5 on pv1.vendorid = pv5.vendorid and pv5.rn=5
left outer join paymentvendors pv6 on pv1.vendorid = pv6.vendorid and pv6.rn=6
left outer join paymentvendors pv7 on pv1.vendorid = pv7.vendorid and pv7.rn=7
left outer join paymentvendors pv8 on pv1.vendorid = pv8.vendorid and pv8.rn=8
left outer join paymentvendors pv9 on pv1.vendorid = pv9.vendorid and pv9.rn=9
left outer join paymentvendors pv10 on pv1.vendorid = pv10.vendorid and pv10.rn=10
 
where pv1.rn=1

Open in new window

0
 

Author Comment

by:asimillc
ID: 22752870
All data is contained within one table.  Does this solution join to the same table ?
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 total points
ID: 22752875
Yes.  The solution above uses a common table expression to express a 1-10 value for rows and then left joins to the cte many times over to "pivot" the data.
0
 

Author Closing Comment

by:asimillc
ID: 31507613
Thanks for the quick help!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

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