我有一个简单的 2 列表(varchar):
name |occupation
___________________
Sandra |Medic
Julija |Actor
Marija |Actor
Martyna |Manager
Aiste |Teacher
Kristina|Teacher
Virg |Teacher
Jurga |Actor
Justina |Medic
Mia |Manager
我需要构建一个看起来像这样的数据透视表:
Medic | Actor | Teacher | Manager |
____________________________________
Justina| Julija| Aiste | Martyna
Sandra | Marija| Kristina| Mia
null | Jurga | Virg | null
我编写了这段代码来返回构建枢轴:
SELECT *
FROM (
SELECT
[name],
[occupation]
FROM employee
) as Results
PIVOT (
max([name])
FOR [occupation]
IN (
[Medic],
[Actor],
[Teacher],
[Manager]
)
) AS PivotTable
但结果我只得到一行:
Medic |Actor |Teacher |Manager
Sandra Marija Virginija Mia
我在哪里做错了?我也无法理解哪种聚合函数更适用,因为行仅包含文本(varchar)。