Since you need to aggregate string values, then you will need to use either the max
or min
aggregate function. The problem with that is it will return a single row for each column. In order to rerun multiple rows, then you will need to use a windowing function like row_number()
to generate a unique value for each id
/string
combination. This will allow you to return multiple rows for each id
:
select Data2 = [2], Data3 = [3], Data4 = [4]
from
(
select id, stringvalue,
row_number() over(partition by id order by stringvalue) seq
from yourtable
) d
pivot
(
max(stringvalue)
for id in ([2], [3], [4])
) piv