简体   繁体   中英

How to transpose row value as column in sql server 2008?

My table structure is like this:

在此处输入图片说明

But I want output as follows:

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

I want to display subjectid as column headings and paper names as rows.

Please help me.

You could try a query like the following one if classid does not matter.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM