1

I'm trying to display exam results form simple database containing two tables tblStudents and tblExamResults.

tblstudents contains student ID and Full_Name columns

In tblexamResults columns are Student_id, Subject and Marks.

as in below Picture

tblstudents

1- Currently I am displaying Student results using this query

SELECT     tblStudents.Full_Name, tblExamResults.Subject, tblExamResults.Marks
FROM         tblExamResults INNER JOIN
                      tblStudents ON tblExamResults.Student_id = tblStudents.Student_ID
order by tblStudents.Full_Name

2 - And results looks like in the following picture:

enter image description here

3 - But what I want is to display each subject as row and get result of each subject below it

So that each student's result is displayed in the same row:

Student_Name sub1_result sub2_Result sub3_Result

Like in the following picture (Excel screenshot)

enter image description here

So:

  • How I can display data in that format?
  • Is that possible in SQL Server?
2
  • 2
    search for CROSS TAB, PIVOT and DYNAMIC SQL Commented Nov 15, 2016 at 9:35
  • Use dynamic pivot concept Commented Nov 15, 2016 at 9:42

2 Answers 2

2
select fullname,[english] english, [history] history, [physics] physics
from 
(
  select fullname,subject,marks
  from (yourquery)
) src
pivot
(
  max(marks)
  for subject in ([english], [history], [physics])
) piv;

or

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.subject) 
            FROM (yourquery) c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT fullname, ' + @cols + ' from 
            (
                select fullname,subject,marks
                from (your query)
           ) x
            pivot 
            (
                 max(marks)
                for subject in (' + @cols + ')
            ) p '


execute(@query)
fullname english history physics
    a   85  70  60
    i   60  100 89
    s   90  90  99
Sign up to request clarification or add additional context in comments.

Comments

0

Finally I've used next part of @Chanukya Answer with little Changes in that Answer i was getting error at line 5 becouse of parentheses in FROM (yourquery) c

Declare @query nvarchar(max);

DECLARE @cols AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.subject) 
            FROM tblExamResults c   ' parentheses Removed from (tblExamResults) c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'');
       print(@cols)
     set @query = '
SELECT *
FROM (
   SELECT     tblStudents.Full_Name, tblExamResults.Subject, tblExamResults.Marks
FROM         tblExamResults INNER JOIN
                      tblStudents ON tblExamResults.Student_id = tblStudents.Student_ID
) as s
PIVOT
(
    sum(marks) FOR subject IN ('+ @cols +')
)AS pvt'
;
execute(@query)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.