SQL to retrieve aggregated data with computed columns

Posted by Remnant on Stack Overflow See other posts from Stack Overflow or by Remnant
Published on 2010-04-11T19:38:28Z Indexed on 2010/04/11 19:43 UTC
Read the original article Hit count: 407

Filed under:
|

I have a table that looks like this for about ~30 students:

StudentID    Course*      CourseStatus
1            Math         Pass
1            English      Fail
1            Science      Pass
2            Math         Fail
2            English      Pass
2            Science      Fail
etc.

*In my actual database the 'Course' column is a CourseID e.g. (1 = Math; 2 = English etc.) which references a 'CourseName' table. I amended the table above just to make it clear the nature of the problem.

I want to write a query (stored procedure) in SQL that summarises performance for a given course and returns the following:

EXEC usp_GetCourseSummary 'Math'

Total Students     Total Pass    % Pass    Total Fail    % Fail
25                 15            60        10            40

Have been scratching my head on this one for some time. Any ideas?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005