0

I have a table with ID's in Column 1 and Codes in Column 2. I'm Looking to get a list of the ID's with the code combination in the next column. Example of the data can be found below:

ID  Code
211  901
211  902
211  903
208  605
208  606

The Result I want would look like this, with the code combinations in one column:

ID     Code_Combo
211    901/902/903
208    605/606

Any ideas? Pretty lost here.

Brian Francke
  • 35
  • 1
  • 4

1 Answers1

2

Here is an example that uses your values. I used the XML PATH example outlined in the first answer of this post.

CREATE TABLE #Test (ID int, Code int)

INSERT INTO #Test (ID, Code) VALUES
(211, 901),
(211, 902),
(211, 903),
(208, 605),
(208, 606)

SELECT
    ID,
    SUBSTRING(Codes, 2, LEN(Codes)) AS 'Codes'
FROM
    (
        SELECT DISTINCT ID,
            (
                SELECT '/' + CAST(Code AS varchar(10))
                FROM #Test AS MTI
                WHERE MTI.ID = MTO.ID
                ORDER BY MTI.ID
                FOR XML PATH ('')
            ) Codes
        FROM #Test AS MTO
    ) AS Final

DROP TABLE #Test
Chris Albert
  • 2,462
  • 8
  • 27
  • 31