我的表结构是这样的:
但我想要输出如下:
English hindi telugu drawing
------- ----- ------- --------
english hindi telugu drawing
paper2 paper1 paper2 drawing2
我希望将subjectid
显示为列标题,将paper names
显示为行。
请帮帮我。
答案 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;