Solved

Multiple rows to single row with multiple columns

Posted on 2011-03-15
1
505 Views
Last Modified: 2012-08-14
I have a table that has information for users associated with the same company on MS SQL 2005

CompanyID | Name   |  Address  |  City   | State   |   Zip   |  Phone
         2          Name1    Address1   City1   State1    Zip1    phone1
         2          Name2    Address2  City2   State2    Zip2    phone2
         3          Name3    Address3  City3   State3    Zip3    phone3

I need to grab the users on the same company and return the data on a single row as follows

Name1 | Address1 | City1 | State1 | Zip1 |  Name2 | Address2 | City2 | State2 |  Zip2
Name1   Address1   City1   State1   Zip1   Name2   Address2   City2   State2   Zip2

Notice that the column names are numbered.
Is it possible to create a query to dynamically generate the column names with a number depending on the number of users associated to the company id (no more than 5 per company) and have a single row with all the results?

This is needed to populate a PDF that only accepts 1 row results from the query. the PDF has fields

Name1    Address1   City1   State1    Zip1    phone1
Name2    Address2  City2   State2    Zip2    phone2

Any help will be greatly appreciated.
0
Comment
Question by:Benjamin_
1 Comment
 
LVL 17

Accepted Solution

by:
dbaSQL earned 500 total points
ID: 35144148
Not sure if this is exactly what you're looking for, but.... it works.
There are a couple very good single-line result set examples here:  http://www.sqlfingers.com/p/tsql-tips.html
--create table with your data
create table company (companyID int, name varchar(15),Address varchar(35),City varchar(20),State char(6),zip char(6),phone varchar(10))
insert company (companyID,name,Address,City,State,zip,phone)
select 2,'Name1','Address1','City1','State1','Zip1','phone1'
union
select 2,'Name2','Address2','City2','State2','Zip2','phone2'
union
select 3,'Name3','Address3','City3','State3','Zip3','phone3'

--check data, make sure it's good
select * from company

--get back records for the same companyID in one line
SELECT 
    a.name [Name1],a.Address [Address1],a.City [City1],a.state [State1],a.zip [Zip1],
    b.name [Name2],b.Address [Address2],b.City [City2],b.state [State2],b.zip [Zip2]
FROM 
    dbo.Company a INNER JOIN dbo.company b
     ON a.companyID = b.companyID
     AND (a.name < b.Name)

--result
Name1	Address1	City1	State1	Zip1  	Name2	Address2	City2	State2	Zip2

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

16 Experts available now in Live!

Get 1:1 Help Now