Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Combining multiple rows into columns Expand / Collapse
Author
Message
Posted Monday, April 7, 2014 1:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 10, 2014 7:33 AM
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.
Post #1559244
Posted Monday, April 7, 2014 1:58 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 4:21 PM
Points: 3,924, Visits: 8,915
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1559260
Posted Monday, April 7, 2014 2:40 PM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 13,230, Visits: 12,709
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 Moden's 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)
Post #1559276
Posted Tuesday, April 8, 2014 6:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, April 10, 2014 7:33 AM
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.
Post #1559465
Posted Tuesday, April 8, 2014 7:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:10 AM
Points: 13,230, Visits: 12,709
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 Moden's 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)
Post #1559475
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse