【问题标题】:Help with PIVOT帮助 PIVOT
【发布时间】:2009-12-31 00:06:55
【问题描述】:

我有一张如下表

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

谢谢

【问题讨论】:

  • 我们怎么知道哪一组词是A1哪组是A2?

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


【解决方案1】:
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
;

【讨论】:

    【解决方案2】:
    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
    

    【讨论】:

    • 这只会给你一行。您需要额外的字段来提供额外的行。
    • -1 这个答案只会返回一行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 2011-06-20
    相关资源
    最近更新 更多