Solved

Looking to pivot grades table

Posted on 2013-06-24
6
277 Views
Last Modified: 2014-01-13
I have a table that stores student grades:
StudentID
CourseCode
CourseName
TermCode
TeacherName
GradeEarned

A student could be given multiple grades for the same course throughout the year with the TermCode being the key.

I'd like to have a SQL statement that pivots this table and returns the data horizontally like a gradebook view:
StudentID----CourseName---TeacherName----Q1--Q2--Q3--Q4
123435          AlgebraI            Mr. Smith            A     B+   B     A-
123435          History              Mr. Jones             C     C+   B-    B

The list of possible TermCodes is known so that doesn't need to be dynamic and I do only need to return the four columns of potential term grades.

js
0
Comment
Question by:jswanberg
  • 3
6 Comments
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
Comment Utility
select
  StudentID
--, CourseCode
, CourseName
, max(case when TermCode = 'Q1' then GradeEarned end) as Q1
, max(case when TermCode = 'Q2' then GradeEarned end) as Q2
, max(case when TermCode = 'Q3' then GradeEarned end) as Q3
, max(case when TermCode = 'Q4' then GradeEarned end) as Q4
from YourTable
group by
  StudentID
--, CourseCode
, CourseName

Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
this is a "classic pivot" using case when syntax. As your TermCode's are a known list, and you only require 4 grade columns, this seems completely appropriate.

Note, sometimes there is a concern about using MAX(...) to achieve these pivots, but it should not be an issue - provided there is only one grade given per student/course/term

The use of MAX() in such a scenario really just allows for reduction from multiple rows to a single row.
0
 
LVL 11

Expert Comment

by:Louis01
Comment Utility
--Prepare sample table
declare @theTable table(  StudentID int
						, CourseName varchar(50)
						, TermCode char(2)
						, GradeEarned varchar(2));

insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (1, 'Course 1', 'Q1', 'A');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (1, 'Course 2', 'Q1', 'B');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (1, 'Course 1', 'Q1', 'C');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (1, 'Course 2', 'Q1', 'D');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (2, 'Course 1', 'Q1', 'A');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (2, 'Course 2', 'Q1', 'B');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (2, 'Course 1', 'Q1', 'C');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (3, 'Course 2', 'Q1', 'D');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (5, 'Course 1', 'Q1', 'A');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (6, 'Course 2', 'Q1', 'B');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (7, 'Course 1', 'Q1', 'C');
insert into @theTable (StudentID, CourseName, TermCode, GradeEarned) values (8, 'Course 2', 'Q1', 'D');

--Pivot table
select StudentID, CourseName
	 , [Q1], [Q2], [Q3], [Q4]
  from (select * from @theTable) t1
 pivot (MAX(GradeEarned) FOR TermCode in ([Q1], [Q2], [Q3], [Q4])) as pt1
 

Open in new window

0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
Comment Utility
Oracle Syntax (11g) for use of the pivot feature, if it is preferred:
SELECT *
FROM   (SELECT STUDENTID,COURSENAME, TERMCODE, GRADEEARNED
        FROM   YOURTABLE)
PIVOT  (MAX(GRADEEARNED) AS GRADE FOR (TERMCODE)  IN ('Q1' AS Q1, 'Q2' AS Q2, 'Q3' AS Q3, 'Q4' AS q4));

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now