views:

59

answers:

2
+1  Q: 

Help with PIVOT

I have a table as follows

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

I want to pivot the above table to get the following result

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

Thanks

A: 
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
stackoverflowuser
This'll only give you one row. You need the extra field to provide extra rows.
Rob Farley
-1 This answer would only result in a single row being returned
Gabriel McAdams
+4  A: 
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
;
Rob Farley