如何在sql server 2008中将行值转换为列?

时间:2016-11-09 09:04:27

标签: sql sql-server

我的表结构是这样的:

enter image description here

但我想要输出如下:

English  hindi  telugu  drawing
-------  ----- ------- --------
english  hindi  telugu  drawing
paper2   paper1  paper2  drawing2

我希望将subjectid显示为列标题,将paper names显示为行。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

如果classid无关紧要,您可以尝试类似下面的查询。

SELECT [English], [Hindi],[Telugu],[Drawing]
FROM 
(
   SELECT ROW_NUMBER() OVER (PARTITION BY SubjectId ORDER BY PaperName) AS rownum, SubjectId, PaperName 
   FROM T
   GROUP BY SubjectId, PaperName 
)AS base
PIVOT
( MAX(PaperName) FOR SubjectId IN ([English], [Hindi],[Telugu],[Drawing])) AS pvt;