0

I'm looking for a sql query that will find all values in "PartNumber below" and concat. using a comma if the material is listed multiple times with different sales orgs. I've been racking my brain trying to figure it out. I'm running SQL 2008 R2

Assume the following SQL table

PartNumber      Org
ABC             1
DEF             2
FGH             3
ABC             2
FGH             5

My expected output would be:

PartNumber     Org
ABC            1,2
DEF            2
FGH            3,5

1 Answers1

0

You can use XML PATH for getting the desired result.

SELECT PartNumber , STUFF(( SELECT  ','+ org FROM t1 a
WHERE b.PartNumber = a.PartNumber FOR XML PATH('')),1 ,1, '')  org
FROM t1 b
GROUP BY PartNumber;
Kryptonian
  • 860
  • 3
  • 10
  • 26