我有一张桌子。
+----------+
| Tools |
+----------+
| Pencil |
| Eraser |
| Notebook |
| Pencil |
| Notebook |
| Notebook |
+----------+
我可以将它转换成这样的东西吗?
+---------+----------+---------+
| No name | No name | No Name | <-- column header
+---------+----------+---------+
| Pencil | Notebook | Eraser | <-- values
+---------+----------+---------+
尝试pivot
查询,但我不认为我做对了。这些工具可能没有修复。我的意思是,也许他们会为它添加另一种工具。
任何帮助将非常感激。感谢
答案 0 :(得分:1)
declare @t table(tools varchar(50))
insert into @t values ('Pencil'),('Eraser'),('Notebook'),('Pencil'),('Notebook'),('Notebook')
select * from @t
SELECT *
FROM (
select
tools
from @t
group by tools
) AS t
PIVOT
(
MAX(tools)
FOR tools IN(Pencil, Eraser, Notebook)
) AS p;