SQL to join multiple rows data

Posted on 2012-08-21
Last Modified: 2012-09-28
Hi all

I have the following table:

CustomerID      Support      
1            Training
1            Remote
1            Telephone
2            Remote
2            Internet

I would like to return in a SQL view the following:

CustomerID      Support
1            Training, Remote, Telephone
2            Remote, Internet

So basically I need to join the initial table into a string.

Thanks for your help
Question by:andrew_optimum
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    please read this article for one of the possible options:

    it's using the FOR XML subquery technique
    LVL 100

    Expert Comment

    Are you doing this for a Crystal Report?  
    If so you can use formulas to concatenate the values and display the result in a group footer

    LVL 45

    Expert Comment

    Hi Andrew,

    The exact answer depends a lot on which SQL you're using.  (Sounds silly, I know.)

    If you have no more than 3 values for each ID, joining the rows to themself by ID is one way.  Using SQL Server pivot is another.  And recursive SQL is still another.

    You might want to read this article for an understanding of recursive SQL:

    Good Luck,
    LVL 18

    Accepted Solution

    I assume the table name is CustomerSupport . Replace the name before to run  the SQL
    SELECT cst.customerid, 
             Stuff((SELECT ', ' + c.Support FROM CustomerSupport c WHERE c.customerid = cst.customerid For XML PATH ('')),1,1,'') as Support
    FROM  (SELECT DISTINCT customerid FROM CustomerSupport ) cst

    Open in new window

    You can also check this thread:

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now