Combining multiple rows into columns

  • 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.

  • 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
  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • 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.

  • 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply