Getting a count of correct and incorrect test attempts

Posted on

Problem

I need to get a count of correct and incorrect attempts per testID:

select
    AttempID,
    Test_ID,
    Date,
    SubjectID,
    Date,
    sum(Correct) as Correct,
    sum(InCorrect) as InCorrect 
from tbldemo
group by
    AttempID,
    Test_ID,
    Date,
    SubjectID_name,
    Date   

Do I need to group by each column other column, and then sum over Correct and Incorrect? AttempID, Test ID, date and subject ID combinations will remain constant.

  AttempID  Test ID   Date      Subject ID  Correct  Incorrect  
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          1       0
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          1       0
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          1       0
   1          2      2014-05-12     2          1       0
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          1       0
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          0       1
   1          2      2014-05-12     2          0       1
   3          4      2014-05-12     2          0       1
   3          4      2014-05-12     2          1       0
   3          4      2014-05-12     2          1       0
   3          4      2014-05-12     2          0       1
   3          4      2014-05-12     2          0       1
   3          4      2014-05-12     2          0       1
   3          4      2014-05-12     2          1       0

Solution

You don’t need the second Date in your select or grouping. Other than that, the query itself looks fine.

I’m a bit more worried about how your table is built, though.

  • There appear to be duplicate records. How would you go about selecting a specific record?

  • Correct and Incorrect are functionally dependent: if one has 0, the other must have 1.

If you have control over the structure of this table, you may want to look into normalising it.

Leave a Reply

Your email address will not be published. Required fields are marked *