2009-12-31 45 views
2

我有一個表如下幫助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 

感謝

+0

怎麼辦我們知道哪一組詞是A1,哪一組是A2? – 2009-12-31 00:14:44

回答

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 
; 
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 
+0

這隻會給你一行。您需要額外的字段來提供額外的行。 – 2009-12-31 00:21:45

+0

-1這個答案只會導致返回一行 – 2009-12-31 00:22:24