2

I have a table like this:

ID    Name
----------
1   john
1   molly
2   greg
2   sean
1   holly
2   mill

What should the SQL Query be to aggregate results like the following:

ID  Name
-------------
1   john/molly/holly
2   greg/sean/mill

1 Answer 1

4

Note: The STUFF function simply removes the leading / from the string returned.

SELECT t1.id, 
       STUFF((SELECT '/' + t2.name
                FROM YourTable t2
                WHERE t1.id = t2.id
                ORDER BY t2.name
                FOR XML PATH('')),1,1,'') AS Name
    FROM YourTable t1
    GROUP BY t1.id
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.