Solved

Multiple rows to single row with multiple columns

Posted on 2011-03-15
1
506 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

932 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

15 Experts available now in Live!

Get 1:1 Help Now