SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Combining multiple rows into columns


Combining multiple rows into columns

Author
Message
meltingchain
meltingchain
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 75
I would like to create a query that will join two tables and have the differences into columns instead of new rows.

I have a table with a companies ID and their address. In the other table i have the companies ID along with a contacts name.

Here is some sample data I made up to help get my point across.

   Declare @CompanyAddress Table (companyID int, CompanyAddress char(30), CompanyState char(2))
insert into @CompanyAddress values(1, '11 14th Ave', 'NY')
insert into @CompanyAddress values(2, '228 108 street', 'NY')

Declare @CompanyContact Table (companyID int, CompanyContact char (30))
insert into @CompanyContact values(1, 'John Smith')
insert into @CompanyContact values(1, 'Amanda Adams')
insert into @CompanyContact values(1, 'Kyle Anderson')
insert into @CompanyContact values(2, 'Stan Queen')
insert into @CompanyContact values(2, 'Tim Joy')



i need the output to be similar to this, however the column title can be anything:


CompanyID CompanyAddress CompanyState CompanyContact CompanyContact CompanyContact
1 11 14th Ave NY John Smith Amanda Adams Kyle Anderson
2 228 108 street NY Stan Queen Tim Joy Null



I researched some before posting. I know a pivot table would be able to do this, but ive never used a pivot table so if you could guide me to a good tutorial or tell me how it works Ill'd greatly appreciate it.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17504 Visits: 19122
You might want to take a look at these articles:
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/

The first one covers the basics and the second one covers a dynamic approach that you might need.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27414 Visits: 17557
Big Kudos!!! You are new around here and you posted everything so nicely. This makes it simple and easy to work on the issue. As Luis said, the articles are crosstabs are a gold mine. One thing that can be a little difficult to figure out is how to make it work for a situation like yours. At first glance you don't have anything to aggregate on so you probably sit there scratching your head wondering how to make this work.

You did such an awesome job posting the details I couldn't help myself showing you the code to do this in a static type of situation.


with MySortedData as
(
select ca.companyID,
ca.CompanyAddress,
ca.CompanyState,
cc.CompanyContact,
ROW_NUMBER()over(partition by ca.CompanyID order by cc.CompanyContact) as RowNum
from @CompanyAddress ca
join @CompanyContact cc on cc.companyID = ca.companyID
)

select companyID, CompanyAddress, CompanyState,
max(case when RowNum = 1 then CompanyContact end) as CompanyContact1,
max(case when RowNum = 2 then CompanyContact end) as CompanyContact2,
max(case when RowNum = 3 then CompanyContact end) as CompanyContact3
from MySortedData
group by companyID, CompanyAddress, CompanyState



If you need a more dynamic approach (you don't know how many columns you need to end up with) you will need to reference the article about dynamic cross tabs. You can find that reference in Luis' post or in my signature.

Hope this helps.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
meltingchain
meltingchain
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 75
Thank you Luis, the articles were new to me and I'm always looking for something to learn.

Also thank you Sean, this example was exactly what I was looking for.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27414 Visits: 17557
meltingchain (4/8/2014)
Thank you Luis, the articles were new to me and I'm always looking for something to learn.

Also thank you Sean, this example was exactly what I was looking for.


You are welcome. Glad that helped and thanks for letting us know.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search