Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multiple rows to single row with multiple columns

Posted on 2011-03-15
1
Medium Priority
?
516 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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

610 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