I had a problem where I need to pivot on a string column. I found the solution in the link below, but cannot understand why pivoting works for column with string data when row_number is used.
Need to Pivot String values in SQL server
Table definition:
ColumnName DataType
---------- --------
Occupation String
Name String
Data:
Occupation Name
---------- ----
Developer A
Developer B
Designer X
Coder Y
Coder Z
The query used was:
SELECT [Developer],
[Designer],
[Coder]
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY (SELECT NULL))
RN
FROM
#temp
) as t
PIVOT
(
MAX(Name)
FOR Occupation
IN ([Developer],[Designer],[Coder])
) as pvt
Output:
Developer Designer Coder
A X Y
B NULL Z