开发者

Help with PIVOT

开发者 https://www.devze.com 2022-12-15 07:18 出处:网络
I have a table as follows Name|Words Awords for A1 h开发者_如何学Goere Bwords for B1 here Cwords for C1 here

I have a table as follows

Name        |          Words
A              words for A1 h开发者_如何学Goere
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


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
;


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号