我有一个如下数据集:
ID Cat
1 Large
1 Small
1 Medium
2 Small
2 Medium
3 Large
我想将其转换为:
ID Small Medium Large
1 Small Medium Large
2 Small Medium NULL
3 NULL NULL Large
我尝试了许多不同的方法,但它们似乎不起作用
感谢您的帮助
答案 0 :(得分:2)
这是一个奇怪的期望输出,但唉:
SELECT ID,
MIN(CASE WHEN Cat = 'Small' THEN 'Small' END) Small,
MIN(CASE WHEN Cat = 'Medium' THEN 'Medium' END) Medium,
MIN(CASE WHEN Cat = 'Large' THEN 'Large' END) Large
FROM YourTable
GROUP BY ID
答案 1 :(得分:2)
<强> SQL Fiddle Demo 强>
SELECT *
FROM MyTable
PIVOT(MAX(Cat) FOR Cat IN (Small,Medium,Large) ) p