troubleshooting Question

Group multiple records into one record

Avatar of cgcmq
cgcmqFlag for Canada asked on
Microsoft SQL ServerSQL
9 Comments1 Solution409 ViewsLast Modified:
I have an SQL Server database that contains the following three tables:

MS_LocalSources which holds information about companies such as name and address
MS_Categories which contains a list of company types
MS_DistCatJoin which relates the MS_LocalSources primary key (id) to one or more records in the MS_Categories table


I need to find each category that a company is associated with.  Normally this would simply be a matter of:

SELECT t1.Lname, t2.catid
FROM MS_LocalSources T1
LEFT JOIN MS_DistCatJoin t2 ON t1.id = Distid
WHERE t1.id = 8

which would return:

Lname            catid
ACME Inc      2
ACME Inc      5
ACME Inc      9

However, in this instance I would like to have only 1 record per company returned with a field that contains each catid as a string:

ACME Inc      2, 5, 9

Can this de done and if so how?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros