This should work. With the ROW_NUMBER()
you will create the header values, which are missing from your table.
As for the aggregation bit, you can see in the answers of the linked post that “You can use the MAX aggregate, it would still work. MAX of one value = that value“
select
test_id
,[1] as val1
,[2] as val2
,[3] as val3
,[4] as val4
from (
select
test_id
,val
,seq = ROW_NUMBER() over (PARTITION by test_id order by test_id asc)
from @tbl
) t
pivot (
max(val)
for seq in ([1], [2], [3], [4])
)p
solved SQL rows to columns without aggregation [duplicate]