Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 717861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:27:38+00:00 2026-05-14T05:27:38+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:27:38+00:00Added an answer on May 14, 2026 at 5:27 am

    Try this:

    CREATE PROCEDURE usp_GetCourseSummary
        @Course nvarchar(100)
    AS     
        SET NOCOUNT ON;
        SELECT
            COUNT(StudentId) AS [Total Students],
            SUM(CASE WHEN CourseStatus = 'Pass' THEN 1 ELSE 0 END) AS [Total Pass],
            SUM(CASE WHEN CourseStatus = 'Pass' THEN 1 ELSE 0 END) * 100 / COUNT(*) AS [% Pass],
            SUM(CASE WHEN CourseStatus = 'Fail' THEN 1 ELSE 0 END) AS [Total Fail], 
            SUM(CASE WHEN CourseStatus = 'Fail' THEN 1 ELSE 0 END) * 100 / COUNT(*) AS [% Fail]
        FROM table1
        WHERE Course = @Course;
    GO
    
    EXEC usp_GetCourseSummary 'Math'
    

    Result:

    Total Students  Total Pass  % Pass  Total Fail  % Fail
    2               1           50      1           50
    

    Test data:

    CREATE TABLE Table1 (StudentID INT NOT NULL, Course NVARCHAR(100) NOT NULL, CourseStatus NVARCHAR(100) NOT NULL);
    INSERT INTO Table1 (StudentID, Course, CourseStatus) VALUES
    (1, 'Math', 'Pass'),
    (1, 'English', 'Fail'),
    (1, 'Science', 'Pass'),
    (2, 'Math', 'Fail'),
    (2, 'English', 'Pass'),
    (2, 'Science', 'Fail');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in MySQL that looks like this: studentID subjectID anotherID anotherID2
I have a table that looks like this: nid vid tid 1 2 3
I have a table that looks like this: <table> <thead> <!-- random table headings
I have a table that looks like this: |StreetName NR| NR | |Teststreet 34|
I have a table that looks like this: id count 1 100 2 50
So I have a table that looks like this: ProjectID TaskID Date 000 001
I have a table that looks something like this: word big expensive smart fast
I have an HTML table that looks like this: ------------------------------------------------- |Column 1 |Column 2
I have a table that looks something like this: id | firstperson | secondperson
I have a LaTeX table that looks like this: \begin{table}[!ht] \centering \small \caption{ \bf{Caption}}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.