1

I looked up several answers that didn't seem to quite work for me, or I used them incorrectly with my query.

Current query:

SELECT
    PartNo,
    SubPartNo
FROM
    Materials

Current Results:

+--------+-----------+
| PartNo | SubPartNo |
+--------+-----------+
|    541 |      5451 |
|    545 |      5451 |
+--------+-----------+

Desired Results:

+----------+-----------+
|  PartNo  | SubPartNo |
+----------+-----------+
| 541, 545 |      5451 |
+----------+-----------+

The result doesn't necessarily need the comma. I am sure this is an easy fix but my limited know-how is slowing me down.

1 Answers1

0

After comments directing me to https://sqlandme.com/2011/04/27/tsql-concatenate-rows-using-for-xml-path/ my question was answered.

SELECT DISTINCT
    matl1.SubPartNo,
    STUFF((
            SELECT ', ' + matl2.PartNo
            FROM Materials as matl2
            WHERE matl2.SubPartNo = matl1.SubPartNo
            FOR XML PATH('')
            ),1,1,'')
FROM
    Materials as matl1