You don't need to transpose rows into columns. You need to concatenate [SODetailId] column values by distinct [SOHeaderId] into a string separated by commas. One of the simplest ways to do this is to use
XML PATH('').
Your query should be something like this:
SELECT DISTINCT [SOHeaderId],
STUFF((SELECT ',' + CAST([SODetailId] AS VARCHAR(10))
FROM [TableName] t1
WHERE t1.[SOHeaderId] = t2.[SOHeaderId]
FOR XML PATH('')), 1, 1, '')
FROM [TableName] t2
I am assuming that the data type of [SODetailId] is
INT, so I am casting it to
VARCHAR(10).
References:
1.
STUFF AND FOR XML PATH for String Concatenation[
^]
2.
Using FOR XML PATH and STUFF to CONCATENATE[
^]