Solved

Multiple rows to single row with multiple columns

Posted on 2011-03-15
1
510 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_
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 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