代码之家  ›  专栏  ›  技术社区  ›  stackoverflowuser

Pivot帮助

  •  2
  • stackoverflowuser  · 技术社区  · 16 年前

    我有一张桌子如下

    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 回复  |  直到 16 年前
        1
  •  5
  •   Rob Farley    16 年前
    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
  •  0
  •   stackoverflowuser    16 年前
    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
    
    推荐文章