SQL Pivot Standards Grades

wendy_r
PowerSchool Champion
PowerSchool Champion

SQL Pivot Standards Grades

Has anyone been able to write a SQL statement to extract standards grades and pivot the columns? Here is my code:

SELECT
st.student_number,
st.grade_level,
st.last_name,
st.first_name,
cc.course_number,
cc.dateenrolled,
cc.dateleft,
CASE WHEN sd.identifier in ('CC.0.M.CC.1') THEN sgs.standardgrade
ELSE ''
END||','||
CASE WHEN sd.identifier in ('CC.0.M.CC.2') THEN sgs.standardgrade
ELSE ''
END||','||
CASE WHEN sd.identifier in ('CC.0.M.G.1') THEN sgs.standardgrade
ELSE ''
END||','||
CASE WHEN sd.identifier in ('CC.0.M.MD.1') THEN sgs.standardgrade
ELSE ''
END||','||
CASE WHEN sd.identifier in ('CC.0.M.MD.2') THEN sgs.standardgrade
ELSE ''
END||','||
CASE WHEN sd.identifier in ('CC.0.M.NBT.1') THEN sgs.standardgrade
ELSE ''
END||','||
CASE WHEN sd.identifier in ('CC.0.M.OA.2') THEN sgs.standardgrade
ELSE ''
END as "Case"
FROM CC
JOIN students st ON st.id = cc.studentid
JOIN sections sec ON sec.id = cc.sectionid
JOIN standardgradesection sgs ON sgs.studentsdcid = st.dcid and sgs.sectionsdcid = sec.dcid
JOIN standard sd ON sd.standardid = sgs.standardid
WHERE cc.termid >= 3000
AND cc.schoolid = 7323
AND sgs.storecode = 'T1'
AND cc.course_number IN ('231120','237110','231110')
AND sd.identifier in ('CC.0.M.CC.1','CC.0.M.CC.2','CC.0.M.G.1','CC.0.M.MD.1','CC.0.M.MD.2','CC.0.M.NBT.1','CC.0.M.OA.2')
ORDER BY st.lastfirst
/

 

I need to pivot the Identifier and StandardGrade into columns. Example: Identifier with grade listed in the column.

cc.0.m.cc.1                   cc.0.m.cc.2

3                                    1

 

Any help is much appreciated! TIA!

---------------------------------------------------------------------------------

Wendy Reynolds, Database Administrator
Avon Grove School District
375 S Jennersville Road, West Grove, PA 19390
0 Replies