帮助PIVOT

时间:2009-12-31 00:06:56

标签: sql sql-server sql-server-2005 tsql pivot

我有一张表如下

Name        |          Words
A              words for A1 here
B              words for B1 here
C               words for C1 here
A               words for A2 here
B               words for B2 here
C               words for C2 here

我想转动上表以获得以下结果

A                    |      B                 |       C
words for A1 here       words for B1 here         words for C1 here
words for A2 here       words for B2 here         words for C2 here

由于

2 个答案:

答案 0 :(得分:5)

With Numbered as
(
select *, ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Words) AS RowNum
from yourTable)
select [A],[B],[C]
from Numbered n
pivot (max(Words) for Name in ([A],[B],[C])) p
;

答案 1 :(得分:0)

select A, B, C from
(
  select Name, CAST(Words as nvarchar(1000)) as Words from DemoTable
) up
pivot (Max(words) for Name in (A, B, C)) as pvt