I have a table of data that looks like:
#test
| RecordID | Name | hasSpanishVersion | Type | TypeID |
|---|---|---|---|---|
| 1 | Test One | Yes | FormType1 | 1 |
| 1 | Test One | Yes | FormType2 | 2 |
| 3 | Test Three | No | null | null |
| 4 | Test Four | Yes | FormType3 | 3 |
| 5 | Test Five | Yes | FormType3 | 3 |
I also have another table that looks like:
#formTypes
| TypeID | FormType |
|---|---|
| 1 | FormType1 |
| 2 | FormType2 |
| 3 | FormType3 |
What I am trying to do is condense the Type column where there are like-RecordIDs / Names. If "hasSpanishVersion" is null, the following two columns will also be null.
I am wanting the example table to look like:
| RecordID | Name | hasSpanishVersion | Type |
|---|---|---|---|
| 1 | Test One | Yes | FormType1, FormType2 |
| 3 | Test Three | null | null |
| 4 | Test Four | Yes | FormType3 |
| 5 | Test Five | Yes | FormType3 |
I have tried the following code, but this only takes all of the FormTypes and condenses them for each of the three different types:
SELECT
*,
STUFF((SELECT '; ' + t.formTypeSpanish
FROM #test t
WHERE t.TypeID = ft.TypeID
FOR XML PATH('')), 1, 1, '') as FormTypes
FROM #formTypes ft
GROUP BY ft.TypeID, ft.FormType
ORDER BY 1